C++ STL 向量 Reserve() 和 copy()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1128535/
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
STL vector reserve() and copy()
提问by natersoz
Greetings,
你好,
I am trying to perform a copy from one vector (vec1) to another vector (vec2) using the following 2 abbreviated lines of code (full test app follows):
我正在尝试使用以下 2 行缩写代码(完整的测试应用程序如下)执行从一个向量 (vec1) 到另一个向量 (vec2) 的复制:
vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), vec2.begin());
While the call to vec2 sets the capacity of vector vec2, the copying of data to vec2 seems to not fill in the values from vec1 to vec2.
虽然调用 vec2 设置了向量 vec2 的容量,但将数据复制到 vec2 似乎没有填充从 vec1 到 vec2 的值。
Replacing the copy() function with calls to push_back() works as expected.
用对 push_back() 的调用替换 copy() 函数按预期工作。
What am I missing here?
我在这里缺少什么?
Thanks for your help. vectest.cpp test program followed by resulting output follows.
谢谢你的帮助。vectest.cpp 测试程序和结果输出如下。
Compiler: gcc 3.4.4 on cygwin.
编译器:cygwin 上的 gcc 3.4.4。
Nat
纳特
/**
* vectest.cpp
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec1;
vector<int> vec2;
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
vec1.push_back(4);
vec1.push_back(5);
vec1.push_back(6);
vec1.push_back(7);
vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), vec2.begin());
cout << "vec1.size() = " << vec1.size() << endl;
cout << "vec1.capacity() = " << vec1.capacity() << endl;
cout << "vec1: ";
for( vector<int>::const_iterator iter = vec1.begin(); iter < vec1.end(); ++iter ) {
cout << *iter << " ";
}
cout << endl;
cout << "vec2.size() = " << vec2.size() << endl;
cout << "vec2.capacity() = " << vec2.capacity() << endl;
cout << "vec2: ";
for( vector<int>::const_iterator iter = vec2.begin(); iter < vec2.end(); ++iter ) {
cout << *iter << endl;
}
cout << endl;
}
output:
输出:
vec1.size() = 7
vec1.capacity() = 8
vec1: 1 2 3 4 5 6 7
vec2.size() = 0
vec2.capacity() = 7
vec2:
回答by rlbond
If the vectors are of the same type, use copy construction or copy assignment:
如果向量的类型相同,则使用复制构造或复制赋值:
vec2(vec1);
vec2 = vec1;
If the vectors aren't the exact same (maybe a different allocator or something, or vec1 is a deque), what you really want is the range-based constructor or range-based assign:
如果向量不完全相同(可能是不同的分配器或其他东西,或者 vec1 是双端队列),您真正想要的是基于范围的构造函数或基于范围的分配:
vec2(vec1.begin(), vec1.end()); // range-based constructor
vec2.assign(vec1.begin(), vec1.end()); // range-based assignment
If you insist on doing it with std::copy
, the proper method is:
如果您坚持使用std::copy
,正确的方法是:
copy(vec1.begin(), vec1.end(), back_inserter(vec2));
Since reserving the space does not make it assignable. copy
works by assigning each element to its new value. So vec2.size()
needs to be at least as large as vec1.size()
in your case. Calling reserve
doesn't actually change a vector's size, just its capacity.
由于保留空间并不能使其可分配。copy
通过将每个元素分配给它的新值来工作。所以vec2.size()
需要至少和vec1.size()
你的情况一样大。调用reserve
实际上并没有改变向量的大小,只是改变了它的容量。
In the book Effective STL, Scott Meyers argues that nearly all uses of std::copy for insertion should be replaced with range-based member functions. I suggest you pick up a copy, it's a great reference!
在Effective STL一书中,Scott Meyers 认为几乎所有使用 std::copy 进行插入都应该替换为基于范围的成员函数。我建议你拿起一本,它是一个很好的参考!
回答by GManNickG
As noted in other answers and comments, you should just use vector's built-in functionality for this. But:
正如其他答案和评论中所述,您应该为此使用 vector 的内置功能。但:
When you reserve()
elements, the vector will allocate enough space for (at least?) that many elements. The elements do not exist in the vector, but the memory is ready to be used. This will then possibly speed up push_back()
because the memory is already allocated.
当您添加reserve()
元素时,向量将为(至少?)那么多元素分配足够的空间。向量中不存在元素,但内存已准备好使用。这可能会加快速度,push_back()
因为内存已经分配。
When you resize()
the vector, it will allocate enough space for those elements, but also add them to the vector.
当你resize()
使用 vector 时,它会为这些元素分配足够的空间,但也会将它们添加到 vector 中。
So if you resize a vector to 100, you can access elements 0 - 99, but if you reserve 100 elements, they are not inserted yet, just ready to be used.
因此,如果将向量大小调整为 100,则可以访问 0 - 99 的元素,但如果保留 100 个元素,则它们尚未插入,只是准备使用。
What you want is something like this:
你想要的是这样的:
vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), std::back_inserter(vec2));
std::back_inserter
is defined in <iterator>
回答by EFraim
Why not: vec2 = vec1;
?
为什么不:vec2 = vec1;
?
回答by jkeys
Change reserve to resize():
将保留更改为 resize():
vec2.resize(vec1.size(), 'v2.insert(v2.end(), v1.begin(), v1.end());
');
copy(vec1.begin(), vec1.end(), vec2.begin());
I believe that is the fix you need.
我相信这是你需要的修复。
I can't give you a very good description on the difference, but basically reserve() makes sure you have enough space, and resize() actually inserts something in there.
我不能给你一个很好的差异描述,但基本上reserve()确保你有足够的空间,resize()实际上在那里插入了一些东西。
回答by etham
In my opinion, the simplest way is to use the std::vector::insert
method:
在我看来,最简单的方法是使用std::vector::insert
方法:
(see std::vector::insert)