C++ 连接两个 std::vectors

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/201718/
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 13:40:24  来源:igfitidea点击:

Concatenating two std::vectors

c++vectorstlconcatenationstdvector

提问by Robert Gamble

How do I concatenate two std::vectors?

如何连接两个std::vectors?

回答by Robert Gamble

vector1.insert( vector1.end(), vector2.begin(), vector2.end() );

回答by Alex

If you are using C++11, and wish to move the elements rather than merely copying them, you can use std::move_iteratoralong with insert (or copy):

如果您使用的是 C++11,并且希望移动元素而不是仅仅复制它们,您可以std::move_iterator与插入(或复制)一起使用:

#include <vector>
#include <iostream>
#include <iterator>

int main(int argc, char** argv) {
  std::vector<int> dest{1,2,3,4,5};
  std::vector<int> src{6,7,8,9,10};

  // Move elements from src to dest.
  // src is left in undefined but safe-to-destruct state.
  dest.insert(
      dest.end(),
      std::make_move_iterator(src.begin()),
      std::make_move_iterator(src.end())
    );

  // Print out concatenated vector.
  std::copy(
      dest.begin(),
      dest.end(),
      std::ostream_iterator<int>(std::cout, "\n")
    );

  return 0;
}

This will not be more efficient for the example with ints, since moving them is no more efficient than copying them, but for a data structure with optimized moves, it can avoid copying unnecessary state:

对于带有整数的示例,这不会更有效,因为移动它们并不比复制它们更有效,但是对于具有优化移动的数据结构,它可以避免复制不必要的状态:

#include <vector>
#include <iostream>
#include <iterator>

int main(int argc, char** argv) {
  std::vector<std::vector<int>> dest{{1,2,3,4,5}, {3,4}};
  std::vector<std::vector<int>> src{{6,7,8,9,10}};

  // Move elements from src to dest.
  // src is left in undefined but safe-to-destruct state.
  dest.insert(
      dest.end(),
      std::make_move_iterator(src.begin()),
      std::make_move_iterator(src.end())
    );

  return 0;
}

After the move, src's element is left in an undefined but safe-to-destruct state, and its former elements were transfered directly to dest's new element at the end.

移动后,src 的元素处于未定义但可安全销毁的状态,其前元素在最后直接转移到 dest 的新元素。

回答by Tom Ritter

I would use the insert function, something like:

我会使用插入功能,例如:

vector<int> a, b;
//fill with data
b.insert(b.end(), a.begin(), a.end());

回答by Roger Lipscombe

Or you could use:

或者你可以使用:

std::copy(source.begin(), source.end(), std::back_inserter(destination));

This pattern is useful if the two vectors don't contain exactly the same type of thing, because you can use something instead of std::back_inserter to convert from one type to the other.

如果两个向量不包含完全相同类型的事物,则此模式很有用,因为您可以使用 something 代替 std::back_inserter 从一种类型转换为另一种类型。

回答by Deqing

With C++11, I'd prefer following to append vector b to a:

使用 C++11,我更喜欢将向量 b 附加到 a:

std::move(b.begin(), b.end(), std::back_inserter(a));

when aand bare not overlapped, and bis not going to be used anymore.

whenab不重叠,b不再使用。



This is std::movefrom <algorithm>, not the usualstd::movefrom <utility>.

这是std::move来自<algorithm>,不是通常的std::move来自<utility>

回答by James Curran

std::vector<int> first;
std::vector<int> second;

first.insert(first.end(), second.begin(), second.end());

回答by ST3

I prefer one that is already mentioned:

我更喜欢已经提到的一个:

a.insert(a.end(), b.begin(), b.end());

But if you use C++11, there is one more generic way:

但是如果你使用 C++11,还有一种更通用的方法:

a.insert(std::end(a), std::begin(b), std::end(b));


Also, not part of a question, but it is advisable to use reservebefore appending for better performance. And if you are concatenating vector with itself, without reserving it fails, so you always should reserve.

此外,这不是问题的一部分,但建议reserve在附加之前使用以获得更好的性能。如果您将 vector 与自身连接起来,而不保留它会失败,那么您总是应该reserve.



So basically what you need:

所以基本上你需要什么:

template <typename T>
void Append(std::vector<T>& a, const std::vector<T>& b)
{
    a.reserve(a.size() + b.size());
    a.insert(a.end(), b.begin(), b.end());
}

回答by Jarod42

With range v3, you may have a lazyconcatenation:

使用范围 v3,您可能有一个惰性连接:

ranges::view::concat(v1, v2)

Demo.

演示

回答by Boris

You should use vector::insert

你应该使用vector::insert

v1.insert(v1.end(), v2.begin(), v2.end());

回答by Vikramjit Roy

A general performance boostfor concatenate is to check the size of the vectors. And merge/insert the smaller one with the larger one.

连接的一般性能提升是检查向量的大小。并将较小的与较大的合并/插入。

//vector<int> v1,v2;
if(v1.size()>v2.size()) {
    v1.insert(v1.end(),v2.begin(),v2.end());
} else {
    v2.insert(v2.end(),v1.begin(),v1.end());
}