views:

116

answers:

4

ive defined the following and filled it with elements:

vector <vector<double> > my_vector;

but i want a delete an element with a specific key...

my_vector.erase(int(specific_key));

but it doesnt allow me. how would i properly dispose of the elements assigned to that key properly?

+1  A: 

erase takes in an iterator as argument.

You can do

my_vector.erase (my_vector.begin() + specific_key);

You can also pass in a range

my_vector.erase (my_vector.begin(), my_vector.begin() + 2);

One thing that you should note is that the size of the vector also gets reduced.

rxin
A: 

The erase method takes iterators as their argument.

Example

Tereno
+2  A: 

Assuming by specific_key you mean the element at that position in the vector:

my_vector.erase(my_vector.begin() + specific_key);

Would be the "most correct" answer.

If you meant to delete the element that matches specific_key (which will have to be of type vector<double> in the given example:

my_vector.erase(find(my_vector.begin(), my_vector.end(), specific_key));
Matt Joiner
yes i want to delete the element at that position in the vector. when i apply the code you gabe (first one), my application freezes.
Prodigga
never mind the freeze was caused by something else. will test this once i get the freezing fixed.
Prodigga
+1, for both the options. Be sure that you check for end() before erasing.
aJ
A: 

If Specific key is not position and say its some data in vector, then one has to iterate vector for that data and erase particular iterator.

Vivek