Home »
C++ STL
valarray size() Function in C++ with Examples
C++ valarray size() Function: Here, we will learn about the size() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on May 13, 2022
The valarray class in C++ is a special container that is used for holding elements like an array and performing operations on them.
C++ STL std::valarray::size() Function
The size() function of the valarray class is used to return the size of the valarray. The function counts the total number of elements present in the valarray and returns the count.
Syntax
size_t size() const;
// or
valarrayName.size();
Parameter(s)
It does not require any parameter.
Return value
It returns an integer value which is the count of elements of the valarray.
Example 1
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<int> myvalarr = { 0, 16, 729, 100, 1 };
// Printing the elements of valarray
cout << "The elements of orignal valarray are : ";
for (int& ele : myvalarr)
cout << ele << " ";
// Finding the size of valarray
int size = myvalarr.size();
cout << "\nThe size of valarray is " << size;
return 0;
}
Output
The elements of orignal valarray are : 0 16 729 100 1
The size of valarray is 5
Example 2
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<double> myvalarr = { 0.313, 16, 729, 100, 1.32, -1, -13.431, 32, 3.43 };
// Printing the elements of valarray
cout << "The elements of orignal valarray are : ";
for (double& ele : myvalarr)
cout << ele << " ";
// Finding the size of valarray
int size = myvalarr.size();
cout << "\nThe size of valarray is " << size;
return 0;
}
Output
The elements of orignal valarray are : 0.313 16 729 100 1.32 -1 -13.431 32 3.43
The size of valarray is 9