Related Articles

# valarray size() function in C++

• Last Updated : 23 Oct, 2018

The size() function is defined in valarray header file. This function is used to find the size of valarray and returns the size of valarray.

Syntax:

`size_t size() const;`

Parameter: This function doesn’t takes any parameter.

Returns: This function returns the number of element in valarray.

Below programs illustrate the above function:

Example 1:-

 `// C++ program to demonstrate``// example of size() function.`` ` `#include ``using` `namespace` `std;``int` `main()``{`` ` `    ``// Initializing  valarray``    ``valarray<``int``> varr = { 20, 40, 60, 80 };`` ` `    ``// Displaying size of valarray``    ``cout << ``"The size of valarray is: "``;``    ``cout << varr.size();`` ` `    ``cout << endl;`` ` `    ``return` `0;``}`
Output:
```The size of valarray is: 4
```

Example 2:-

 `// C++ program to demonstrate``// example of size() function.`` ` `#include ``using` `namespace` `std;`` ` `int` `main()``{`` ` `    ``// Initializing  valarray``    ``valarray<``int``>``        ``varr = { -20, 40, -50, 60, 80, 0, 0 };`` ` `    ``// Displaying size of valarray``    ``cout << ``"The size of valarray is: "``;``    ``cout << varr.size();`` ` `    ``cout << endl;`` ` `    ``return` `0;``}`
Output:
```The size of valarray is: 7
```
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up