c – std :: vector和std :: array初始化列表之间的区别

c – std :: vector和std :: array初始化列表之间的区别,第1张

概述这个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(){ fo 这个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;}

它打印出六个预期值.

但如果我改变了矢量< str>对于数组< str,3>,gcc给了我这个错误:“std :: array’的初始化器太多了”.

如果我改变了字段的初始化:

array<str,3> fIElds {    str{1,str{3,str{5,6}};

事情很顺利.

那么为什么我在使用std :: array时需要str {1,而在使用std :: vector时只需要{1,2}?

解决方法 请参阅 aggregate initialization的cppreference部分.

The effects of aggregate initialization are:

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.

这意味着如果你的结构中有一个聚合,例如:

struct str {    struct asdf    {        int first,last;    } asdf; };

asdf将由第一个嵌套的brace-init-List初始化,即{{1,2}}.你通常需要两对大括号的原因是因为嵌套的brace-init-List初始化了std :: array中的底层聚合(例如,T a [N]).

但是,您仍然可以像这样初始化数组:

array<str,3> fIElds {    1,2,3,4,5,6};

要么:

array<str,3> fIElds { {    1,6} };

代替.

另一方面,如何初始化向量由list initialization覆盖.std::vector有一个接受std::initializer_list的构造函数.

The effects of List initialization of an object of type T are:

Otherwise,the constructors of T are consIDered,in two phases:

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 type std::initializer_List

请注意,您将无法初始化矢量(如下所示:

vector<str> fIElds {    1,6};

但:

vector<int> fIElds {    1,6};

很好.

总结

以上是内存溢出为你收集整理的c – std :: vector和std :: array初始化列表之间区别全部内容,希望文章能够帮你解决c – std :: vector和std :: array初始化列表之间的区别所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1241737.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存