c++ - 如何一次向 std::vector 添加多个值?

标签 c++ loops vector

<分区>

我需要能够多次将同一个变量绑定(bind)在一起,而不管它是哪种数据类型。

我有什么:

utils::milliseconds first_variable = utils::seconds(10.1)

utils::milliseconds second_variable = utils::seconds(2.4)

std::vector<utils::milliseconds>{first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, first_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, second_variable, };

我怎样才能做到这一点,这样我就不必输入同一个变量 100 次了?

最佳答案

您可以使用循环将多个值添加到 vector 中,这是一个示例:

int foo = 2;
int bar = 3;

std::vector<int> vector;

for (int i = 0; i < 2; i++)
{
  vector.push_back(foo);
}

for (int i = 0; i < 3; i++)
{
  vector.push_back(bar);
}

结果将是一个 vector ,看起来等同于使用这个:

std::vector{foo, foo, bar, bar, bar};

解释:

for(int i = 0; i < 2; i++)是一个只要条件 i < 2 就运行的循环是真的。 i初始化为零并递增 1每次循环运行时,循环将运行2次。

vector.push_back(foo)附加变量 foo到 vector 。

更好的解决方案:

正如@Peter 在评论中指出的那样,std::vector可以使用它的构造函数或 insert() 填充元素功能:

int foo = 2;
int bar = 3;

std::vector<int> vector(2, foo);
vector.insert(vector.end(), 3, bar);

std::vector构造函数有两个参数,第一个是大小(你想在 vector 中包含多少个元素)和一个应该用来填充所有元素的值。

insert()方法接受三个参数,第一个指定插入的位置,它可以是vector.begin()。或 vector.end() .第二个参数是要插入的元素数量,第三个参数指定应该用于填充元素的值。

关于c++ - 如何一次向 std::vector 添加多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59069696/

相关文章:

c++ - 我在编写一个控制台应用程序来创建一个简单的程序来解决退休金的数学方程式时遇到问题

c++ - getline() 返回错误

c++ - 信号与信号2

javascript - 在 JavaScript 中执行 While 循环

c++ - 传递一个空的 vector 参数

c++ - 如何在 "std::vector<char>"容器中查找单个单词

c++ - 使用任何缓冲区来存储任何数据,并放置 new 和 memcpy

c - 当超出 C 中的整数范围时会发生无限循环

c++ - 程序不等待 cin

c++ - 对c++ vector 的交集