×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Printing all elements of a vector in C++ STL

C++ STL | printing all elements of a vector: Here, we are going to learn how to print all elements of a vector using vector::begin() and vector::end() function in C++ STL?
Submitted by IncludeHelp, on May 09, 2019

Printing all elements of a vector

To print all elements of a vector, we can use two functions 1) vector::begin() and vector::end() functions.

vector::begin() function returns an iterator pointing to the first elements of the vector.

vector::end() function returns an iterator point to past-the-end element of the vector.

We run a loop from the first element to the less than past-the-element and prints the vector elements.

Note: To use vector, include <vector> header.

C++ STL program to print all elements of a vector

//C++ STL program to print all elements of a vector 
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //creating iterator
    vector<int>::iterator it;

    //printing all elements
    cout << "vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

vector v1 elements are: 10 20 30 40 50

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.