使用硬编码元素初始化std :: vector最简单的方法是什么? [英] What is the easiest way to initialize a std::vector with hardcoded elements?
本文介绍了使用硬编码元素初始化std :: vector最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以这样创建一个数组并初始化:
I can create an array and initialize it like this:
int a[] = {10, 20, 30};
如何创建 std :: vector
并初始化它相似的优雅?
How do I create a std::vector
and initialize it similarly elegant?
我最好的方法是:
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);
推荐答案
< >解决方案
解决方案
一种方法是使用数组初始化向量
One method would be to use the array to initialize the vector
static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
这篇关于使用硬编码元素初始化std :: vector最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文