<分区>
我正在关注 this tutorial on move semantics当我遇到以前从未见过的东西时:没有参数的 vector.reserve()。我去了documentation对于该方法,还没有保留(无效)签名。如果我们可以调用 reserve(void),为什么没有记录呢?此外,该调用的作用是什么?为什么我们要在实例化一个空 vector 后进行调用?
#include <iostream>
using namespace std;
vector<int> doubleValues (const vector<int>& v)
{
vector<int> new_values;
new_values.reserve();
for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr; ++itr )
{
new_values.push_back( 2 * *itr );
}
return new_values;
}
int main()
{
vector<int> v;
for ( int i = 0; i < 100; i++ )
{
v.push_back( i );
}
v = doubleValues( v );
}