微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这个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
};

很好.

原文地址:https://www.jb51.cc/c/119428.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐