ADVERTISEMENT
ADVERTISEMENT

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.

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.

C++ valarray size() Function 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

C++ valarray size() Function 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

ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.