Jump to content
  • Advertisement
Sign in to follow this  
KKTHXBYE

lame question how to delete something from vector

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!