分配所有值(具有相同值)std::array:括号 vs .fill,有什么区别?

huangapple go评论83阅读模式
英文:

assign all values (with the same value) a std:array: brace vs .fill, what's the differences?

问题

如果我需要将相同的值分配给数组中的所有值,大括号赋值和.fill之间有什么区别?

#include <iostream>
#include <array>

int main()
{
    std::array<int, 4> array;

    array[3] = 2;
    array = {0};
    std::cout << array[3] << std::endl;

    array[3] = 2;
    array.fill(0);
    std::cout << array[3] << std::endl;
}

它们看起来一样...
英文:

If I need to assign to an array the same values on all values, what's the differences between brace assign and .fill?

#include &lt;iostream&gt;
#include &lt;array&gt;

int main()
{
    std::array&lt;int, 4&gt; array;

    array[3] = 2;
    array = {0};
    std::cout &lt;&lt; array[3] &lt;&lt; std::endl;

    array[3] = 2;
    array.fill(0);
    std::cout &lt;&lt; array[3] &lt;&lt; std::endl;
}

They looks the same...

答案1

得分: 4

对于std::array<int, 4> array

  • array = {0} -> 0,0,0,0
  • array = {2} -> 2,0,0,0
  • array = {2,4} -> 2,4,0,0
  • array = {2,4,27,-3} -> 2,4,27,-3
  • array = {2,4,27,-3,1} -> 编译错误

编辑:最终找到了一个合适的参考资料(也许我眼瞎,但我在cppreference.com上找不到它):

来自C++26工作草案:

9.4.2 聚合体[dcl.init.aggr]

3 当聚合体由初始化器列表初始化,如9.4.5所规定,初始化器列表的元素被视为聚合体元素的初始化器。聚合体的显式初始化元素如下确定:

—(3.1) 如果初始化器列表是花括号括起的指定初始化器列表,那么聚合体必须是类类型,每个指示符中的标识符必须命名类的直接非静态数据成员,并且聚合体的显式初始化元素是那些是或包含这些成员的元素。

—(3.2) 如果初始化器列表是花括号括起的初始化器列表,则聚合体的显式初始化元素是初始化器列表中元素的前n个元素,其中n是初始化器列表中的元素数。

—(3.3) 否则,初始化器列表必须为{},并且没有显式初始化元素。

5 对于非联合聚合体,未显式初始化的每个元素如下初始化:

—(5.1) 如果元素具有默认成员初始化器(11.4),则从该初始化器初始化元素。

—(5.2) 否则,如果元素不是引用,则从空初始化器列表(9.4.5)进行复制初始化。

—(5.3) 否则,程序非法。

9.4.5 列表初始化[dcl.init.list]

[...]

(3.11) 否则,如果初始化器列表没有元素,则对象被值初始化。

我认为这是与此问题相关的所有部分(但我不习惯直接阅读标准,所以欢迎更正)。

英文:

For std::array&lt;int, 4&gt; array:

  • array = {0} -> 0,0,0,0
  • array = {2} -> 2,0,0,0
  • array = {2,4} -> 2,4,0,0
  • array = {2,4,27,-3} -> 2,4,27,-3
  • array = {2,4,27,-3,1} -> compile error

EDIT: Finally found a proper reference (perhaps I'm blind, but I cannot find it on cppreference.com):

From the C++26 working draft:

> ### 9.4.2 Aggregates [dcl.init.aggr]
>
> 3 When an aggregate is initialized by an initializer list as specified
> in 9.4.5, the elements of the initializer list are taken as
> initializers for the elements of the aggregate. The explicitly
> initialized elements of the aggregate are determined as follows:
>
> —(3.1) If the initializer list is a brace-enclosed
> designated-initializer-list, the aggregate shall be of class type, the
> identifier in each designator shall name a direct non-static data
> member of the class, and the explicitly initialized elements of the
> aggregate are the elements that are, or contain, those members.
>
> —(3.2) If the initializer list is a brace-enclosed initializer-list,
> the explicitly initialized elements of the aggregate are the first n
> elements of the aggregate, where n is the number of elements in the
> initializer list.

>
> —(3.3) Otherwise, the initializer list must be {}, and there are no
> explicitly initialized elements.

> 5 For a non-union aggregate, each element that is not an explicitly
> initialized element is initialized as follows:
>
> —(5.1) If the element has a default member initializer (11.4), the
> element is initialized from that initializer.
>
> —(5.2) Otherwise, if the element is not a reference, the element is
> copy-initialized from an empty initializer list (9.4.5).

>
> —(5.3) Otherwise, the program is ill-formed.

> ### 9.4.5 List-initialization [dcl.init.list]
>
> [...]
>
> (3.11) Otherwise, if the initializer list has no elements, the object
> is value-initialized.

I think that is all the relevant parts for this question (but I am not used to reading the standard directly so corrections are welcome).

huangapple
  • 本文由 发表于 2023年7月13日 14:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定