Elegant way for space efficient unpacking of a vector of pairs in two vectors
<p>I have a big vector of <code>std::pair&lt;std::string,size_t&gt;</code> elements and I want to unpack it in two vectors using small extra memory overhead (I don't want the memory space occupation doubled, i.e. erasing the vector of pairs after the unpacking) and possibly as fast as possible. The following solution is unacceptably slow:</p>

<pre><code>std::vector&lt;std::pair&lt;std::string, size_t&gt;&gt; string_weight;
get_from_file("mybigfile.txt", string_weight); //it just fills the string_weight vector
//... do stuff...
std::vector&lt;std::string&gt; strings;
std::vector&lt;size_t&gt; weights;
for (auto it = string_weight.begin(); it != string_weight.end() ; it = string_weight.erase(it)) {
     strings.push_back(std::move(it-&gt;first));
     weights.push_back(std::move(it-&gt;second));
}
</code></pre>

<p>Therefore I tried to modify the previous solution just changing the for loop in the following way:</p>

<pre><code>for (auto it = string_weight.begin(), it2 = it; it != string_weight.end() ; it = string_weight.erase(it, it2)) {
        size_t delta = 100000;
        for ( it2 = it ; it2 != string_weight.end() &amp;&amp; it2 != it+delta; it2++ ) {
            strings.push_back(std::move(it2-&gt;first));
            weights.push_back(std::move(it2-&gt;second));
        }
    }
</code></pre>

<p>This is faster but the completion time is proportional to the value I choose for <code>delta</code> and I don't like it. Can you help me giving a solution or pointing out some useful technique?</p>

<p>Thank you in advance.</p>