Home » C++ STL

Push and print elements in an integer vector in C++ STL

C++ STL Vector: Here, we are implementing a program in which we are declaring an integer vector, pushing the elements and printing the elements.
Submitted by IncludeHelp, on June 18, 2019

In this example, we are declaring an integer vector, pushing the elements using a for loop with the counter starting value from 1 to 5 and then printing the value separated by space.

Code:

#include <iostream>
#include <vector> //for vector
using namespace std;

int main()
{
    // vector  for integer elements
    vector<int> v1;

    // adding 5 elements
    for (int i = 1; i <= 5; ++i) {
        v1.push_back(i);
    }

    // print all elements
    cout << "vector elements are: ";
    for (int i = 0; i < v1.size(); ++i) {
        cout << v1[i] << ' ';
    }
    cout << endl;

    return 0;
}

Output

vector elements are: 1 2 3 4 5

Related posts






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.