public member function
<array>

std::array::size

constexpr size_type size() noexcept;
Return size
Returns the number of elements in the array container.

The size of an array object is always equal to the second template parameter used to instantiate the array template class (N).

Unlike the language operatorsizeof, which returns the size in bytes, this member function returns the size of the array in terms of number of elements.

参数



返回值

The number of elements contained in the array object.
This is a compile-time constant expression (constexpr).

成员类型size_typeis an alias of the unsigned integral type size_t.

示例

1
2
3
4
5
6
7
8
9
10
11
12
// array::size
#include <iostream>
#include <array>

int main ()
{
  std::array<int,5> myints;
  std::cout << "size of myints: " << myints.size() << std::endl;
  std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;

  return 0;
}

可能的输出
size of myints: 5
sizeof(myints): 20


复杂度

常量。

迭代器有效性

没有变化。

数据竞争

不访问任何包含的元素:并发访问或修改它们是安全的。

异常安全

无异常保证:此成员函数从不抛出异常。

另见