作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 C++ 中,可以使用 int myarray[20]
创建一个预定义大小的数组,例如 20。 .但是,the online documentation on vectors没有显示初始化 vector 的类似方法:相反,应该使用例如 std::vector<int> myvector (4, 100);
初始化 vector .这给出了一个大小为 4 的 vector ,所有元素的值都是 100。
如何像数组一样只使用预定义的大小而没有预定义的值来初始化 vector ?
最佳答案
使用构造函数:
// create a vector with 20 integer elements
std::vector<int> arr(20);
for(int x = 0; x < 20; ++x)
arr[x] = x;
关于c++ - 如何创建用户定义大小但没有预定义值的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559283/
我是一名优秀的程序员,十分优秀!