valarray begin() function in C++

The begin() function is defined in valarray header file. This function returns an iterator pointing to the first element in the valarray v.

Syntax:

template< class T > 
    begin( valarray<T>& v );

Parameter: This function takes a mandatory parameter v which represents the valarray object.



Returns: This function returns the iterator to the first value in the 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 begin() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initializing valarray
    valarray<int> varr = { 10, 20, 30, 40, 50 };
  
    cout << "valarray contains=";
  
    for (auto i = begin(varr); i != end(varr); i++) {
        cout << ' ' << *i;
    }
  
    cout << endl;
  
    return 0;
}

chevron_right


Output:

valarray contains= 10 20 30 40 50

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// example of begin() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Initializing valarray
    valarray<int> varr = { -10, -20, -30, -40 };
  
    cout << "valarray contains=";
  
    for (auto i = begin(varr); i != end(varr); i++) {
        cout << ' ' << *i;
    }
  
    cout << endl;
  
    return 0;
}

chevron_right


Output:

valarray contains= -10 -20 -30 -40


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.