What is the most efficient way to append one std::vector to the end of another?

前端 未结 5 1161
旧巷少年郎
旧巷少年郎 2020-12-13 05:44

Let v1 be the target vector, v2 needs to be appended to the back of it.

I\'m now doing:

v1.reserve(v1.size() + v2.size()); 
copy(v2.begin(), v2.end()         


        
5条回答
  •  执笔经年
    2020-12-13 06:31

    If you happen to use Boost you can download the development version of the RangeEx library from the Boost Vault. This lib. was accepted into Boost a while ago but so far it hasn't been integrated with the main distribution. In it you'll find a new range-based algorithm which does exactly what you want:

    boost::push_back(v1, v2);
    

    Internally it works like the answer given by UncleBens, but the code is more concise and readable.

提交回复
热议问题