C++ 将向量附加到向量的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18147400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:45:46  来源:igfitidea点击:

Best way to append vector to vector

c++c++11vectorappendstd

提问by vdenotaris

std::vector<int> a;
std::vector<int> b;
std::vector<int> c;

I would like to concatenate these three vectors by appending b's and c's elements to a. Which is the best way to do this, and why?

我想通过将b's 和c's 元素附加到a. 哪个是最好的方法,为什么?



1)By using vector::insert:

1)通过使用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)By using std::copy:

2)通过使用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)By using std::move(from C++11):

3)通过使用std::move(来自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();

采纳答案by Xaqq

In my opinion, your first solution is the best way to go.

在我看来,您的第一个解决方案是最好的方法。

vector<>::insertis designed to add element so it's the most adequate solution.

vector<>::insert旨在添加元素,因此它是最合适的解决方案。

You could call reserveon the destination vector to reserve some space, but unless you add a lot of vector together, it's likely that it wont provide much benefits: vector<>::insertknow how many elements will be added, you will avoid only one reservecall.

您可以调用reserve目标向量以保留一些空间,但除非您将大量向量添加在一起,否则它可能不会提供太多好处:vector<>::insert知道将添加多少元素,您将避免只reserve调用一次。

Note: If those were vectorof more complex type (ie a custom class, or even std::string), then using std::movecould provide you with a nice performance boost, because it would avoid the copy-constructor. For a vector of inthowever, it won't give you any benefits.

注意:如果那些是vector更复杂的类型(即自定义类,甚至std::string),那么 usingstd::move可以为您提供很好的性能提升,因为它会避免复制构造函数。int然而,对于一个向量,它不会给你任何好处。

Note 2: It's worth mentioning that using std::movewill cause your source vector's content to be unusable.

注意2:值得一提的是,使用std::move会导致您的源vector内容无法使用。

回答by Michael Goldshteyn

Assuming you want to copy and not move, this would be the best way:

假设你想复制而不是移动,这将是最好的方法:

a.reserve(a.size()+b.size()+c.size()); // Reserve space first
a.insert(a.end(),b.begin(),b.end());
a.insert(a.end(),c.begin(),c.end());

If you want to move:

如果你想搬家:

a.reserve(a.size()+b.size()+c.size()); // Reserve space first
a.insert(a.end(),std::make_move_iterator(b.begin()),
         std::make_move_iterator(b.end()));
a.insert(a.end(),std::make_move_iterator(c.begin()),
         std::make_move_iterator(c.end()));
b.swap(std::vector<int>()); // Clear and deallocate space
c.swap(std::vector<int>()); // Clear and deallocate space

Update: You've edited your question several times now making it somewhat of a moving target. Your first option is now very similar to my first suggestion.

更新:您已经多次编辑您的问题,使其成为一个移动目标。您的第一个选项现在与我的第一个建议非常相似。

Update 2: As of C++11, you may no longer have to use the "swap with empty vector"trick to clear and deallocate space, depending on your library's implementation of vector. The following may do the job in a more intuitive way:

更新 2:从 C++11 开始,您可能不再需要使用“与空向量交换”技巧来清除和释放空间,具体取决于您的库的vector. 以下可能以更直观的方式完成这项工作:

// Empty the vectors of objects
b.clear(); 
c.clear();

// Deallocate the memory allocated by the vectors 
// Note: Unlike the swap trick, this is non-binding and any space reduction
//       depends on the implementation of std::vector
b.shrink_to_fit();
c.shrink_to_fit();

回答by Pete Becker

The first is the best choice because insertcan figure out how many elements it's adding and resize the vector to fit before it starts copying. The others don't have that information, so could end up resizing after some copying, which would be slower than resizing at the start, or resizing more than once.

第一个是最好的选择,因为insert可以在开始复制之前弄清楚它添加了多少元素并调整向量的大小以适应。其他人没有这些信息,所以可能会在一些复制后调整大小,这会比开始时调整大小或多次调整大小要慢。

However, as @michaelgoldshteyn hints, since you're going to do two insertions, you can also resize the array yourself with the final size, potentially saving one resize.

但是,正如@michaelgoldshteyn 所暗示的那样,由于您要进行两次插入,您还可以自己调整数组的最终大小,从而可能节省一次调整大小。

回答by Kyle_the_hacker

If you really want to append the data of band cin vector a, you have to do insertion (which actually is your 1.):

如果您真的想在 vector 中附加band的数据,则必须进行插入(实际上是您的1.):ca

a.reserve( a.size() + b.size() + c.size() ); // preallocate memory (see why)
a.insert( a.end(), b.begin(), b.end() );
a.insert( a.end(), c.begin(), c.end() );

Depending on the compiler std::copy(your 2.) should normally be as fast.

根据编译器std::copy(您的2.)通常应该一样快。

Since a std::vectorhas always to be contiguous in memory, you can't just move(as defined in C++11) and if you know the end size you have to reserve your vector(it will avoid unnecessary reallocations of your vector). But if you really worry about performance, let this as three std::vectorand iterate over them when you have to read their data.

由于 astd::vector在内存中始终是连续的,因此您不能只是移动(如 C++11 中所定义),并且如果您知道最终大小,则必须保留您的向量(这将避免对您的向量进行不必要的重新分配)。但是,如果您真的担心性能,请将其设为三个std::vector,并在您必须读取它们的数据时对其进行迭代。