這個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
?