valarray size() function in C++

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:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// example of size() function.
  
#include <bits/stdc++.h>
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;
}

chevron_right


Output:

The size of valarray is: 4

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// example of size() function.
  
#include <bits/stdc++.h>
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;
}

chevron_right


Output:

The size of valarray is: 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.