How to remove vector nth element in C++? [SOLVED]


Azka Iftikhar

Tips and Tricks

Introduction

In this article we will learn c++ vector remove nth element. Vectors are a part of the c++ STL library. They are containers but can change memory at runtime. Which can be very useful in memory and space management of programs.

 

Vectors

Vectors are a part of Standard Template Library (STL) in C++ which is a general purpose library used to store and retrieve continuous data. Vectors are similar to arrays but they change their size during runtime. C++ and Java are one of the few general purpose high level languages which help in memory management.

 

How to initialize a vector ?

Include the vector library

#include<vector>

Initialize vector

vector<int> v;

Vectors have some built in functions, some most important ones are :

  • push_back() to insert data at the end.
  • begin() shows the starting point of the vector.
  • end() shows the end of the vector.
  • size() returns the size of the vector.
  • capacity() shows the capacity of the vector.
  • resize() it resize the vector to the desirable size.

Lets look at a code example of how all these functions work

// c++ vector remove nth element
#include <vector>
#include <iostream>
using namespace std;
int main()
{
    vector<int> v;
    int i;
    for (i = 0; i < 10; i++)
        v.push_back(i);
    v.erase(v.begin() + 5);
    for (i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    //size of vector
    cout << "size of vector: " << v.size() << endl;
    //capacity of vector
    cout << "capacity of vector: " << v.capacity() << endl;
    //resize vector
    v.resize(5);
    cout << "size of vector: " << v.size() << endl;
    //begin
    cout << "begin: " << *v.begin() << endl;
    //end
    cout << "end: " << *v.end() << endl;
    //max size
    cout << "max size: " << v.max_size() << endl;
    return 0;
}

Output of this code is :

0 1 2 3 4 6 7 8 9 
size of vector: 9
capacity of vector: 16
size of vector: 5
begin: 0
end: 6
max size: 2305843009213693951

 

Solution: c++ vector remove nth element

We will now look at how c++ vector remove nth element . one way is to add the nth element to the start and erase that element.

Lets look at the code :

// c++ vector remove nth element
#include <vector>
#include <iostream>
using namespace std;
int main()
{
    vector<int> v;
    int i;
    for (i = 0; i < 10; i++)
        v.push_back(i);
    for (i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    // // remove the nth element
    int n;
    cout << "Enter the element to be removed: ";
    cin >> n;
    v.erase(v.begin() + n);
    for (i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    return 0;

}

Output : 

The vector is 0 1 2 3 4 5 6 7 8 9 
Enter the element to be removed: 4
0 1 2 3 5 6 7 8 9

 

Conclusion

In this article we learned c++ vector removes nth element. How we can add elements to a vector, how we can access it, how we can erase an element to a vector. We studied the in-built functions of the vectors like size, capacity, begin, and end.

 

Further Reading

Vector Operations 
Vectors

 

Views: 9

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel