【发布时间】:2013-07-02 02:31:47
【问题描述】:
How do you copy the contents of an array to a std::vector in C++ without looping? 有一个很好的例子,如何简单地将数组的内容复制到向量中。
你能用同样的技术将一个向量的一部分复制到另一个向量中吗?
例如
vector<int> v;
// ...v has some number of elements and we're interested in an
// arbitrary number (y) from an arbitrary start point (x)...
vector<int> v2(&v[x], &v[x + y]);
【问题讨论】:
-
如果您确定
v至少有x+y元素,您可以使用该精确符号。但是,请考虑 chris 的答案,它更通用