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


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

不訪問任何包含的元素:併發訪問或修改它們是安全的。

異常安全

無異常保證:此成員函式從不丟擲異常。

另見