我可能会在这里感到困惑,但我所理解的是:
因此,如果我的目的是拥有一个只需要读取而不需要更新的对象,那么我应该只等待我的C++编译器中的移动语义支持?
我对吗 ?
谢谢 :)
我想为我的vector使用一个工厂函数,并且还使用迭代器而不调用resize来打破我以前的值?
可能还是我错过了STL设计的一个观点?
#include <vector>
#include <algorithm>
#include <iostream>
struct A
{
A():_x(42){}
A(double x):_x(x){}
double _x;
};
struct factory
{
A operator()()
{
return A(3.14);
}
};
int main()
{
std::vector<A> v;
int nbr = 3;
v.reserve(nbr);
std::generate_n(v.begin(), nbr, factory());
std::cout << "Good values" << std::endl;
for(int i = 0 ; i < nbr ; ++i)
std::cout << v[i]._x << std::endl;
v.resize(nbr); //How I can have the syntax below without the resize which blows my previous values ?
std::cout << …Run Code Online (Sandbox Code Playgroud)