连接两个向量的最佳方法是什么?

2021-12-21 00:00:00 vector c++

我正在使用多线程并希望合并结果.例如:

I'm using multitreading and want to merge the results. For example:

std::vector<int> A;
std::vector<int> B;
std::vector<int> AB;

我希望 AB 必须按顺序处理 A 的内容和 B 的内容.做这样的事情最有效的方法是什么?

I want AB to have to contents of A and the contents of B in that order. What's the most efficient way of doing something like this?

推荐答案

AB.reserve( A.size() + B.size() ); // preallocate memory
AB.insert( AB.end(), A.begin(), A.end() );
AB.insert( AB.end(), B.begin(), B.end() );

相关文章