Linked Questions
30 questions linked to/from How do I erase an element from std::vector<> by index?
38 votes
5 answers
76k views
Remove ith item from a C++ std::vector [duplicate]
How do I remove the ith item from a std::vector? I know I want to delete the ith element. I have int i; and std::vector<process> pList; where process is a struct. I want to do something ...
9 votes
4 answers
8k views
How to erase multiple elements from std::vector<> by index using erase function? [duplicate]
I have a vector a storing values [0 1 2 3 5] and other vector removelist storing the indexes to be removed [0 1 2] in order to leave [3 5] at the end. When I'm implementing the following code, it ...
4 votes
1 answer
4k views
Removing a pair from a vector of pairs [duplicate]
I have a vector of pairs std::vector<int,double> lambda; I would like to remove the kth element, i.e lambda.erase(&lambda[k]); lambda.erase(lambda[k]); Both of these attempts throws a no ...
-1 votes
2 answers
846 views
c++ 2D vector(matrix) how to delete the nth row? [duplicate]
Here is the 2d vector [[1,3],[2,6],[8,10],[15,18]] I want to delete the 2nd row which is [2,6] I tried following to erase the 1st row matrix[1].erase(intervals[1].begin(),intervals[1].end()); after ...
3 votes
1 answer
266 views
erase() does not work on a STL vector inside a structure/object? [duplicate]
I have an object that contains an STL vector. I start with the vector being size zero and use push_back to add to it. So, push_back works fine. In my code, each element in the vector represents an ...
0 votes
1 answer
372 views
c++ vector<char> erase() error, won't compile [duplicate]
#include <iostream> #include <vector> #include <cstdlib> #include <time.h> using namespace std; void makeVector(); void breakVector(); vector<char> asciiChar; vector&...
-8 votes
1 answer
448 views
Vectors in functions, how to create find and erase function for deleting indexes based on user input (string)? [duplicate]
/* So the teacher built the main and a prototype for a class that builds and modifies a phone book. Right now I'm barely defining the class functions, but ran into trouble defining one of his ...
0 votes
1 answer
552 views
How to delete specific unique_ptr from vector and change vector size [duplicate]
How I can access and print the value of unique_ptr, how to delete the 3rd value and accordingly reduce the container's value without making segmentation fault. #include <memory> #include <...
0 votes
0 answers
383 views
Deleting a specific element in an array of Vectors C++ [duplicate]
I'm quite stuck on this function for a c++ program. Instructions Write a function called RemoveGrades that takes the student array and grade vector as parameters, it asks the user for a valid ...
0 votes
0 answers
71 views
Deleting an object from an vector [duplicate]
I've got a problem : I have a vector vector<TSquare> SquaresList; and when I try to delete an member of it (which is a TSquare instance)delete SquaresList[l]; it says this: main.cpp|499|error: ...
127 votes
12 answers
104k views
Removing item from vector, while in C++11 range 'for' loop?
I have a vector of IInventory*, and I am looping through the list using C++11 range for, to do stuff with each one. After doing some stuff with one, I may want to remove it from the list and delete ...
221 votes
6 answers
29k views
What are the errors, misconceptions or bad pieces of advice given by cplusplus.com? [closed]
There are several references for the C++ standard library, including the invaluable ISO standard, MSDN, IBM, cppreference, and cplusplus. Personally, when writing C++ I need a reference that has quick ...
23 votes
8 answers
14k views
Advance iterator for the std::vector std::advance VS operator +?
I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator it=vec.begin(); /..../ std::advance(it, location); instead of it= it + 5; what is ...
21 votes
6 answers
12k views
Erasing elements in std::vector by using indexes
I've a std::vector<int> and I need to remove all elements at given indexes (the vector usually has high dimensionality). I would like to know, which is the most efficient way to do such an ...
5 votes
3 answers
31k views
Vector Erase Error
I have the following code in C++: #include <iostream> #include <cstdlib> #include <ctime> #include <vector> int main () { srand(time(0)); int noOfElements = 9; for ...