如何一次将多个int传入向量? [英] How do I pass multiple ints into a vector at once?
本文介绍了如何一次将多个int传入向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我必须多次使用 vector.push_back()
。
Currently when I have to use vector.push_back()
multiple times.
我目前使用的代码是
std::vector<int> TestVector;
TestVector.push_back(2);
TestVector.push_back(5);
TestVector.push_back(8);
TestVector.push_back(11);
TestVector.push_back(14);
有没有办法只使用 vector.push_back c $ c>一次,只是将多个值传递给向量?
Is there a way to only use vector.push_back()
once and just pass multiple values into the vector?
推荐答案
尝试传递数组向量:
int arr[] = {2,5,8,11,14};
std::vector<int> TestVector(arr, arr+5);
您可以随时调用 std :: vector :: assign 将数组赋给向量,调用std :: vector :: insert添加多个数组。
You could always call std::vector::assign to assign array to vector, call std::vector::insert to add multiple arrays.
如果您使用C ++ 11,您可以尝试:
If you use C++11, you can try:
std::vector<int> v{2,5,8,11,14};
或
std::vector<int> v = {2,5,8,11,14};
这篇关于如何一次将多个int传入向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文