public member function
<valarray>

std::valarray::size

size_t size() const;
Return size
Returns the number of elements in the valarray.

参数



返回值

The number of elements in the valarray.
size_t 是一个无符号整数类型。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// valarray::size example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray

int main ()
{
  std::valarray<int> myvalarray;
  std::cout << "1. After construction: " << myvalarray.size() << '\n';

  myvalarray = std::valarray<int>(5);
  std::cout << "2. After assignment: " << myvalarray.size() << '\n';

  myvalarray.resize(3);
  std::cout << "3. After downsizing: " << myvalarray.size() << '\n';

  myvalarray.resize(10);
  std::cout << "4. After resizing up: " << myvalarray.size() << '\n';

  return 0;
}

输出

1. After construction: 0
2. After assignment: 5
3. After downsizing: 3
4. After resizing up: 10


复杂度

Depends on library implementation, but likely constant.

迭代器有效性

No changes: Valid iterators, references and sub-arrays keep their validity.

数据竞争

The valarray is accessed, but its elements are not.

异常安全

Unspecified, but this member function shall not throw exceptions for valid valarray objects.

另见