的std :: vector和std ::数组初始化列表之间的区别 [英] Difference between std::vector and std::array initializer lists
问题描述
本C ++ 11 code正常工作对我来说:
This C++11 code works fine for me:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
struct str {
int first, last;
};
vector<str> fields {
{1,2}, {3,4}, {5,6}
};
int main()
{
for (str s : fields)
cout << s.first << " " << s.last << endl;
}
它打印出的六个期望值。
It prints the six expected values.
但是,如果我更改矢量&lt; STR&GT;
到阵列&LT; STR,3 GT;
,海湾合作委员会给我这个错误:太多的初始化为'的std ::阵'
But if I change vector<str>
to array<str,3>
, gcc gives me this error: "too many initializers for ‘std::array’".
如果我更改字段的初始化
这样的:
array<str,3> fields {
str{1,2}, str{3,4}, str{5,6}
};
东西很好地工作。
Things work nicely.
那么,为什么我需要 STR {1,2}当使用
的std ::阵列
,但只有 {1,2}当使用
的std ::矢量
?
So why do I need str{1,2}
when using std::array
, but only {1,2}
when using std::vector
?
推荐答案
请参阅CP preference的部分上的集合初始化。
See cppreference's section on aggregate initialization.
集合初始化的效果是:
-
每个数组元素或非静态类成员,在类定义数组的下标/出场顺序,是复制初始化
初始化列表中相应的条款。
Each array element or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.
如果初始化从句是一个嵌套的支撑,初始化列表,相应的类成员本身就是一个聚集:汇总
初始化是递归的。
If the initializer clause is a nested braced-init-list, the corresponding class member is itself an aggregate: aggregate initialization is recursive.
这意味着,如果你有你的结构内的聚集,如:
This means that if you had an aggregate inside your struct, such as:
struct str {
struct asdf
{
int first, last;
} asdf;
};
航空自卫队
将由第一个嵌套大括号初始化列表,也就是被初始化 {{1,2}}
。为什么你一般需要2对括号的原因是因为嵌套的大括号初始化列表中初始化的std ::阵列
(例如, T一[N]
)。
asdf
would be initialized by the first nested brace-init-list, i.e. { { 1, 2 } }
. The reason why you generally need two pairs of braces is because the nested brace-init-list initializes the underlying aggregate in std::array
(for example, T a[N]
).
不过,你仍然可以初始化阵列是这样的:
However, you can still initialize your array like this:
array<str,3> fields {
1, 2, 3, 4, 5, 6
};
或
array<str,3> fields { {
1, 2, 3, 4, 5, 6
} };
来代替。
在另一方面,你如何初始化向量由列表初始化覆盖 。 的std ::矢量
有一个接受<一个构造函数href=\"http://en.cp$p$pference.com/w/cpp/utility/initializer_list\"><$c$c>std::initializer_list$c$c>.
On the other hand, how you initialize your vector is covered by list initialization. std::vector
has a constructor that accepts an std::initializer_list
.
类型T的对象的列表初始化的效果是:
The effects of list initialization of an object of type T are:
-
另外,T的构造都认为,分两个阶段进行:
Otherwise, the constructors of T are considered, in two phases:
- 称取所有构造
的std :: initializer_list
作为唯一的参数,或者作为第一个参数,如果剩余的参数有
默认值,进行检查,并通过重载决议匹配
针对类型的单个参数的std :: initializer_list
- All constructors that take
std::initializer_list
as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of typestd::initializer_list
请注意,你将无法初始化向量(是这样的:
Note that you wouldn't be able to initialize your vector ( like this:
vector<str> fields {
1,2, 3,4, 5,6
};
但
vector<int> fields {
1,2, 3,4, 5,6
};
是完全没有问题。
is perfectly fine.
这篇关于的std :: vector和std ::数组初始化列表之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!