How do I erase an element from std vector by index

0 votes

I have a std::vectorint> that I want to remove then'th element from. 

How can I go about doing that?

std::vector<int> vec;

vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);

vec.erase(???);
Jun 10, 2022 in C++ by Nicholas
• 7,760 points
1,509 views

1 answer to this question.

0 votes

You might perform the following to remove a single element:

std::vector<int> vec;

vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);

// Deletes the second element (vec[1])
vec.erase(std::next(vec.begin()));

Alternatively, to remove many elements at once:

// Deletes the second through third elements (vec[1], vec[2])
vec.erase(std::next(vec.begin(), 1), std::next(vec.begin(), 3));
answered Jun 10, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

answered Aug 5, 2022 in C++ by Damon
• 4,960 points
985 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
715 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
552 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,163 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
602 views
0 votes
0 answers

Sorting a vector in descending order

Should I  utilize  std::sort(numbers.begin(), numbers.end(), std::greater<int>()); or std::sort(numbers.rbegin(), numbers.rend()); // ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
2,285 views
0 votes
0 answers

Accumulate function in Vector (STL) giving negative sum

I'm receiving a negative total when I run the code below (-294967296). #include<iostream> #include<vector> #include<numeric> using namespace std; int main() { ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
663 views
0 votes
0 answers

Remove elements of a vector inside the loop

I am aware that there are questions like this one, but I was unable to use them to help me decipher my code.  I just want to remove or delete one element from a vector by running a loop over its property.  How can I go about doing that?  I tried the following code, but I got an ambiguous error message: 'operator =' function is unavailable in 'Player’. ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
619 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,791 views
0 votes
1 answer

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
760 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP