如何用范围填充向量?

Wil*_*isa 0 c++ vector

如何在 C++ 中用 1 到 10 的数字填充向量?我有这个,但它不起作用。

vector<int>test(10); test={ 1, 10 };

Luc*_*ore 6

另一个使用generate

vector<int>test(10);
int x = 0;
std::generate(test.begin(), test.end(), [&]{ return x++; }); 
Run Code Online (Sandbox Code Playgroud)


0x4*_*2D2 6

您可以使用std::iota()

std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
Run Code Online (Sandbox Code Playgroud)