C++ STL Vector Function Through programs in 3 minutes

Learn C++ vector functions through programs for competitive programming.

·

3 min read

C++ STL Vector Function Through programs in 3 minutes

TL;DR – Vectors are nothing but arrays that are dynamic in nature which means that vectors possess the ability to change their size anytime during program run. Vectors have two major sets of functions.

MODIFIERS

These modify data elements present in vectors usually result in resizing the same.

#include<bits/stdc++.h>
using namespace std;
void printVector(vector <int> &g1)
{
    for (int i = 0; i < g1.size(); i++)
        cout << g1[i] << " ";
    cout<<""<<endl;
}
int main() {
    // declaring vector
    vector <int> g1;
    vector <int> g2;

// It assigns value 10 , 5 times. g1.assign(no of rep. , value)
    g1.assign(5, 10);
    printVector(g1);

// It adds value at the end of vector.
    g1.push_back(15);
    g1.push_back(25);
    g1.push_back(35);
    printVector(g1);

// It takes out the last value from vector.
    g1.pop_back();
    printVector(g1);

// It inserts value at a given position. g1.insert(position,value)
// g1.begin() points to starting postion.
    g1.insert(g1.begin()+3,7);
    printVector(g1);

// It removes an entry at a given position or in a range of positions. 
//g1.insert(position)
    g1.erase(g1.begin()+1);
    printVector(g1);
//g1.insert(starting_position,ending_position)
    g1.erase(g1.begin(),g1.begin()+2);
    printVector(g1);

// It inserts a value at a given position and shifts rest elements.
    g1.emplace(g1.begin()+2, 3);
    g1.emplace_back(40);
    printVector(g1);

    g2.assign(5, 4);
    printVector(g1);
    cout<<"g1 Before : "<<g1.size()<<endl;
    cout<<"g2 Before : "<<g2.size()<<endl;

    g1.swap(g2);
    cout<<"g1 After : "<<g1.size()<<endl;
    cout<<"g2 After : "<<g2.size()<<endl;


    return 0;
}

Output

10 10 10 10 10 
10 10 10 10 10 15 25 35 
10 10 10 10 10 15 25 
10 10 10 7 10 10 15 25 
10 10 7 10 10 15 25 
7 10 10 15 25 
7 10 3 10 15 25 40 
7 10 3 10 15 25 40 
g1 Before : 7
g2 Before : 5
g1 After : 5
g2 After : 7

ITERATORS

They are fundamentally used to access value of iterator and for traversal.

#include<bits/stdc++.h>
#include <typeinfo>
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    vector <int> g1={ 1, 3, 5, 7, 9 };
    vector<int>::iterator ptr;
    for (ptr = g1.begin(); ptr < g1.end(); ptr++)
        cout << *ptr << " ";
    cout<<""<<endl;
    // ptr is at the end beyond valued position(i.e. 6)

    vector<int>::iterator ptr1= g1.begin();
    advance(ptr1,3);
    cout << *ptr1<<endl;
    // ptr1 is at position=4

    auto it1 = prev(ptr, 4);
    cout << *it1<<endl;
    // it1 is at postion 6-4=2
    auto it = next(it1, 2);
    cout << *it<<endl;
    // // it is at postion 2+2=4

     return 0;
}

Output

1 3 5 7 9 
7
3
7

Three ways to print a Vector

#include<bits/stdc++.h>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    vector <int> g1={ 1, 3, 5, 7, 9 };


    // Ways tp print vector
    cout <<"First Way to print vector "<<endl;
    for (int &x : g1) 
        cout << x << " ";
     cout<<""<<endl;


    cout <<"Second Way to print vector "<<endl;
    for (int i = 0; i < g1.size(); i++)
        cout << g1[i] << " ";
    cout<<""<<endl;

    cout <<"Third Way to print vector "<<endl;
    for (ptr = g1.begin(); ptr < g1.end(); ptr++)
        cout << *ptr << " ";
    cout<<""<<endl;

     return 0;
}

Follow this page for many such tutorials.

Did you find this article valuable?

Support Ayush Raj by becoming a sponsor. Any amount is appreciated!