D
Digital Puer
I would like to merge together several STL integer vectors using STL's
merge() function. What is the best way to do this while avoiding
unnecessary vector copying?
Is the following a good approach?
vector<vector<int> > data; // already filled up properly with integers
vector<int> result; // temp space
vector<int> result2; // temp space
result = data.at(0); // unnecessary copying?
for (int i = 0; i < data.size(); i++)
{
result2.resize(result.size() + data.at(i).size()); // is this needed?
merge(result.begin(), result.end(), data.at(i).begin(),
data.at(i).end(), result2.begin());
result = result2; // unnecessary copying?
}
return result;
merge() function. What is the best way to do this while avoiding
unnecessary vector copying?
Is the following a good approach?
vector<vector<int> > data; // already filled up properly with integers
vector<int> result; // temp space
vector<int> result2; // temp space
result = data.at(0); // unnecessary copying?
for (int i = 0; i < data.size(); i++)
{
result2.resize(result.size() + data.at(i).size()); // is this needed?
merge(result.begin(), result.end(), data.at(i).begin(),
data.at(i).end(), result2.begin());
result = result2; // unnecessary copying?
}
return result;