public member function
<valarray>

std::valarray::size

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

Parameters

none

Return Value

The number of elements in the valarray.
size_t is an unsigned integral type.

Example

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

Output:

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


Complexity

Depends on library implementation, but likely constant.

Iterator validity

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

Data races

The valarray is accessed, but its elements are not.

Exception safety

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

See also