C++ Vectors

C++ Vectors

In the previous article, we discussed C++ Vectors and their implementation. In this article, we will be looking at Vector elements Accessing ad Modifiers.

Vector Element Accessing

There are several methods to access an element of a vector. Let’s look at some of them.

  • Reference operator [i] – Returns a reference to the element at position ‘i’ in the vector.
  • at(i) – Returns a reference to the element at position ‘i’ in the vector.
  • front() – Returns a reference to the first element in the vector.
  • back() – Return a reference to the last element in the vector.
  • data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

Let’s look at an example to use these methods:

// C++ program to illustrate the
// element access in vector
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> g1;
 
    for (int i = 1; i <= 10; i++)
        g1.push_back(i * 10);
 
    cout << "\nReference operator [g] : g1[2] = " << g1[2];
 
    cout << "\nat : g1.at(4) = " << g1.at(4);
 
    cout << "\nfront() : g1.front() = " << g1.front();
 
    cout << "\nback() : g1.back() = " << g1.back();
 
    // pointer to the first element
    int* pos = g1.data();
 
    cout << "\nThe first element is " << *pos;
    return 0;
}

Output:

Reference operator [g] : g1[2] = 30
at : g1.at(4) = 50
front() : g1.front() = 10
back() : g1.back() = 100
The first element is 10

Vector Modifiers

The modifiers are used to manipulate the vector by adding or removing elements. The following are some of the vector modifiers.

  • assign() – It assigns new values to the vector elements by replacing old ones
  • push_back() – It is used to push the elements into a vector from the back
  • pop_back() – It is used to pop the elements from the back of the vector
  • insert() – It inserts new elements before the element at the specified position
  • erase() – It is used to remove elements from a container from the specified position or range
  • swap() – It is used to swap the contents of two vectors of the same type. Sizes may differ
  • clear() – It is used to remove all the elements of the vector container
  • emplace() – It extends the container by inserting a new element at the position specified
  • emplace_back() – It is used to insert a new element into the vector container, and the new element is added to the end of the vector
See also  POLYMORPHISM IN C++

Let’s look at an example of vector manipulation:

// C++ program to illustrate the
// Modifiers in vector
#include <bits/stdc++.h>
#include <vector>
using namespace std;
 
int main()
{
    // Assign vector
    vector<int> v;
 
    // fill the array with 10 five times
    v.assign(5, 10);
 
    cout << "The vector elements are: ";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
 
    // inserts 15 to the last position
    v.push_back(15);
    int n = v.size();
    cout << "\nThe last element is: " << v[n - 1];
 
    // removes last element
    v.pop_back();
 
    // prints the vector
    cout << "\nThe vector elements are: ";
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
 
    // inserts 5 at the beginning
    v.insert(v.begin(), 5);
 
    cout << "\nThe first element is: " << v[0];
 
    // removes the first element
    v.erase(v.begin());
 
    cout << "\nThe first element is: " << v[0];
 
    // inserts at the beginning
    v.emplace(v.begin(), 5);
    cout << "\nThe first element is: " << v[0];
 
    // Inserts 20 at the end
    v.emplace_back(20);
    n = v.size();
    cout << "\nThe last element is: " << v[n - 1];
 
    // erases the vector
    v.clear();
    cout << "\nVector size after erase(): " << v.size();
 
    // two vector to perform swap
    vector<int> v1, v2;
    v1.push_back(1);
    v1.push_back(2);
    v2.push_back(3);
    v2.push_back(4);
 
    cout << "\n\nVector 1: ";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";
 
    cout << "\nVector 2: ";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";
 
    // Swaps v1 and v2
    v1.swap(v2);
 
    cout << "\nAfter Swap \nVector 1: ";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";
 
    cout << "\nVector 2: ";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";
}

Output:

The vector elements are: 10 10 10 10 10 
The last element is: 15
The vector elements are: 10 10 10 10 10 
The first element is: 5
The first element is: 10
The first element is: 5
The last element is: 20
Vector size after erase(): 0

Vector 1: 1 2 
Vector 2: 3 4 
After Swap 
Vector 1: 3 4 
Vector 2: 1 2

That is all for this article. Hope you got a good understanding of vectors and their usage along with different methods used on the vectors.

See also  C++98 features

Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh