像这样初始化 int 向量有什么问题:vector<int> v1 = {1, 2, 3, 4}?

泽尔

当我像这样在 C++ 中初始化一个向量时:

#include <string>
#include <vector>
using std::vector;
using std::string;
int main() {

  vector<int> v1 = {1, 2, 3, 4};
  return 0;
}

我收到此错误:

prog1.cpp:8:15: error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
  vector<int> v1 = {1, 2, 3, 4};
              ^    ~~~~~~~~~~~~
1 error generated.

知道为什么会这样吗?

安尼班166

初始化列表是从 C++ 11 开始引入的,因此-std=version如果您使用旧标准进行默认编译,则需要在编译 ( ) 时指定该(或更高)版本

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么陈述“ vector <int>(v1);” 失败

vector <int> + = 1,1,2,2,2,3,4,5,6如何; 可能?

int q = {1,2}; 特殊的初始化列表

C ++标准对std :: vector <int> v1,v2怎么说?std :: distance(v1.begin(),v2.begin())?

如何初始化“ int a [100] = {1}”

(int_1 + = * pointer ++ = int_2 ++)<int_3是什么意思?

为什么map <bool,int> m = {{1,2,3,4,5,0}} 1号而不是3号?

Int32.Parse与Single.Parse-(“ 1,234”)和(“ 1,2,3,4”)。为什么int和浮点类型解析分隔符的方式不同?

如何创建一个类,使向量起作用std :: vector <MyClass <int >> v {1,2,3};

将 int ([3,6]) 数组转换为 [[1,2,3],[1,2,3,4,5,6]]

必须首先打印vector <int> v1中最接近整数x的数字

ValueError: 基数为 10 的 int() 的文字无效:'1 2 3 4 5'

无法将“{1, 2 ,3, 4, 5, 6}”转换为 list<int> C++?

std :: initializer_list <int>({1,2,3})和{1,2,3}有什么区别?

如何使用int数组[] []初始化向量<vector <int >>?

在Rails 4中创建API-未初始化的常量Api :: V1 :: UsersController

C ++:初始化“ int Agent :: tun_alloc(char *,int)”的参数1 [-fpermissive]

什么是“ int(i)= 1;”?

'template< unsigned N, unsigned M> int compare(char p1 [N], char p2 [M])'有什么问题

为什么不能使用嵌套的初始化程序列表初始化std :: array <std :: pair <int,int>,3>,但是std :: vector <std :: pair <int,int >>可以初始化?

'初始化':无法从'Array <int,3>'转换为'int'

使用`Int((n + 1)/ 2)`,`round(Int,(n + 1)/ 2)或`Int((n + 1)// 2)哪个更好?

_在Int => Int = _ + 1中的用途是什么

Scala中的def f = x:Int-> List(x-1,x,x + 1)有什么问题?

為什麼 'val arr: Int => Int = Array(1,2,3)' 在 Scala 中是允許的

在C#中为什么(char)(1)+(char)(2)导致int 3

拆分数据时出现问题:KeyError: "None of [Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype='int64')] are in [列]"

`int * [1]`和`int(*)[1]`有什么区别?

C ++ Vector <int>数组初始化