c ++向量初始化 [英] c++ vector initialization

查看:259
本文介绍了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项目。我收到以下错误:

本地函数定义是非法的

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 ++向量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆