gpt4 book ai didi

c++ - 如何一次将多个整数传递给一个 vector ?

转载 作者:IT老高 更新时间:2023-10-28 13:57:47 27 4
gpt4 key购买 nike

目前当我必须多次使用 vector.push_back() 时。

我目前使用的代码是

  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() 并将多个值传递给 vector ?

最佳答案

你可以用初始化列表来做到这一点:

std::vector<unsigned int> array;

// First argument is an iterator to the element BEFORE which you will insert:
// In this case, you will insert before the end() iterator, which means appending value
// at the end of the vector.
array.insert(array.end(), { 1, 2, 3, 4, 5, 6 });

关于c++ - 如何一次将多个整数传递给一个 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com