Best way to appending vectors to vector in C++
Assuming I have three vectors:
std::vector<int> a;
std::vector<int> b;
std::vector<int> c;
I would like to concatenate these vectors appending b's and c's elements
to a.
Which is the best way to do that? Why?
1) Using vector::insert:
a.reserve(a.size() + b.size() + c.size());
a.insert(a.end(), b.begin(), b.end());
a.insert(a.end(), c.begin(), c.end());
b.clear();
c.clear();
2) Using std::copy:
a.reserve(a.size() + b.size() + c.size());
std::copy(b.begin(), b.end(), std::inserter(a, a.end()));
std::copy(c.begin(), c.end(), std::inserter(a, a.end()));
b.clear();
c.clear();
3) Using std::move (from C++11):
a.reserve(a.size() + b.size() + c.size());
std::move(b.begin(), b.end(), std::inserter(a, a.end()));
std::move(c.begin(), c.end(), std::inserter(a, a.end()));
b.clear();
c.clear();
Best regards, Vi.
No comments:
Post a Comment