• 11
• 11
• 12
• 10
• 11

# lame question how to delete something from vector

This topic is 961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

so i have std::vector<> list;

now int i;

list.erase(i); doesn't work

Description Resource Path Location Type
no matching function for call to 'std::vector<TEventItem>::erase(int&)' event_chat.h

i just don't understand why i cant simply delete x item on the list...

##### Share on other sites

so i dont get it, does it mean i can erase something that is 1.5 from begin() then?

##### Share on other sites

iterators can only take integers in addition, much like pointers

##### Share on other sites
yeah the topic is wrong, i was concerned about not having an int as index there. Edited by frob
remove profanity and personal attack.

##### Share on other sites

If only there were a website built entirely around answering simple programming questions and only if there were a place you could go to ask questions and have the entire Internet scoured for answers in a matter of milliseconds.

There are many. Here is a good one that I highly recommend: http://www.gamedev.net/index

so i dont get it, does it mean i can erase something that is 1.5 from begin() then?

This is kind of bizarre! What would you expect to get 1.5 from begin()? Edited by MarkS

##### Share on other sites

Do you mean deleting by value instead of index? So, something like

std::vector<float> values = {1.41f, 3.14f, 42.0f}; //Initialize vector with some values
values.erase(1.41f);

for(float v : values)
std::cout << v << std::endl;


And it would say 3.14f and 42.0f?

For that, you could just run through the vector and delete the value:

for(auto it = values.begin(); it != values.end(); it++)
if(*it == 1.41f) {
values.erase(it);
break;
}


Or, include <algorithm> and do this:

values.erase(std::find(values.begin(), values.end(), 1.41f);


std::find returns the iterator to the first item in the range ( values.begin(), values.end() ) that compares equal to the value (1.41f) that you provide.

Please note that in the example, I use floats, so it is hard to confuse the value with an index. However, comparing floats simply with the == operator is usually not optimal. That is an entirely different topic, I advise you to look it up.