c++ vector initialization(c++向量初始化)
问题描述
我一直在 Code::Blocks 和 MingW 编译器中使用以下向量初始化值:
I have been using the following vector initialization with values in Code::Blocks and MingW compiler:
vector<int> v0 {1,2,3,4};
之后,我不得不将代码移至 Visual Studio 项目 (c++) 并尝试构建.我收到以下错误:
局部函数定义是非法的
After that I had to move the code to a visual studio project (c++) and I tried to build. I got the following error:
local function definitions are illegal
Visual Studio 编译器不支持这种初始化?
我需要如何更改代码以使其兼容?
我想初始化向量并同时用值填充它,就像一个数组一样.
Visual Studio compiler does not support this kind of initialization?
How do I need to change the code to make it compatible?
I want to initialize vector and fill it with values at the same time, just like an array.
推荐答案
Visual C++ 尚不支持初始化列表.
Visual C++ does not yet support initializer lists.
最接近此语法的方法是使用数组来保存初始化器,然后使用范围构造函数:
The closest you can get to this syntax is to use an array to hold the initializer then use the range constructor:
std::array<int, 4> v0_init = { 1, 2, 3, 4 };
std::vector<int> v0(v0_init.begin(), v0_init.end());
这篇关于c++向量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c++向量初始化
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
