这个C++代码11工作正常,我:差异表
#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;
}
它打印出的六个预期值。
但是,如果我将vector<str>
更改为array<str,3>
,gcc会给我这个错误:“std :: array'的初始值过多”。
如果我改变的fields
从而初始化:
array<str,3> fields {
str{1,2}, str{3,4}, str{5,6}
};
事情很好地工作。
那么为什么我需要str{1,2}
时使用std::array
,但只有{1,2}
时使用std::vector
?