我试图了解C ++ STL容器的初始化。这是我的噩梦:
vector<int> V0 ({ 10,20 }); // ok - initialized with 2 elements
vector<int> V1 = { 10,20 }; // ok - initialized with 2 elements
vector<int> V2 = {{ 10,20 }}; // ok - initialized with 2 elements
vector<int> V3 = {{ 10 },20 }; // ok - initialized with 2 elements
vector<int> V4 { 10,20 }; // ok - initialized with 2 elements
vector<int> V5 {{ 10,20 }}; // ok - initialized with 2 elements
vector<int> V6 {{ 10 },20 }; // ok - initialized with 2 elements
queue<int> Q0 ({ 10,20 }); // ok - initialized with 2 elements
// queue<int> Q1 = { 10,20 }; // compile error
// queue<int> Q2 = {{ 10,20 }}; // compile error
// queue<int> Q3 = {{ 10 },20 }; // compile error
// queue<int> Q4 { 10,20 }; // compile error
queue<int> Q5 {{ 10,20 }}; // ok - initialized with 2 elements
// queue<int> Q6 {{ 10 },20 }; // compile error
我们正在谈论C ++ 11。
我做了一些研究,这是我的问题:
- 我认为编译错误是由于缺少
queue<T>
的{{1}}构造函数而导致的。请参见initializer_list
:http://www.cplusplus.com/reference/vector/vector/vector/和vector<T>
: http://www.cplusplus.com/reference/queue/queue/queue/我正确吗? - 现在,对于从
queue<T>
到V0
的所有向量,我了解了V6
,V0
,V1
。有人可以帮助我理解V4
,V2
,V3
和V5
吗? - 我不太了解
V6
或Q0
。有人可以帮我吗?
我还在阅读Mike Lui的文章:Initialization in C++ is Seriously Bonkers。我想与大家分享一下,但是有没有一种快速的方法可以帮助我了解这场噩梦? :-)