Sign in to follow this  
ICanC

Find and delete Vector element

Recommended Posts

hi all,

 

I'm learning C++ and trying to understand how to handle vectors, specifically how to find an element of any type in a vector, then delete it.

 

I've been reading through my book which gives lots of theory and short examples, and some google searches have turned up more errors than my own attempts! I'd apprecite a bit of guidance please, here is my test attempt, which fails on the std::find line

#include <iostream>
#include <vector>

void displayvec(std::vector<std::string>&);


int main ()
{
    std::vector<std::string> test {"Knife", "Boat", "Gun", "Water"};
    displayvec(test);
    test.erase(test.begin()+2);
    displayvec(test);
    auto it = std::find(test.begin(), test.end(), "Boat");



    return 0;
}
void displayvec(std::vector<std::string>&v)
{
    for (std::string s : v )
    {
        std::cout << s << std::endl;
    }
}

How do I find and delete Boat?

Share this post


Link to post
Share on other sites
Does it fail because you're not including <algorithm> or does it give an error? When asking for help, it's pretty much always a good idea to give the exact error message you're receiving.

Share this post


Link to post
Share on other sites
#include <algorithm>
#include <iostream>
#include <vector>

void displayvec(std::vector<std::string>&);


int main ()
{
    std::vector<std::string> test {"Knife", "Boat", "Gun", "Water"};
    displayvec(test);
    test.erase(test.begin()+2);
    displayvec(test);
    auto it = std::find(test.begin(), test.end(), "Boat");
    if (it != test.end()) {
      std::cout << "boat found\n";
    }
}
void displayvec(std::vector<std::string>&v)
{
    for (std::string s : v )
    {
        std::cout << s << std::endl;
    }
}

WFM.

Share this post


Link to post
Share on other sites

ah yes, it failed simply because I didn't include algorithm, very sorry

 

solved :

#include <iostream>
#include <vector>
#include <algorithm>

void displayvec(std::vector<std::string>&);


int main ()
{
    std::vector<std::string> test {"Knife", "Boat", "Gun", "Water"};
    displayvec(test);
    test.erase(test.begin()+2);
    displayvec(test);
    auto it = std::find(test.begin(), test.end(), "Boat");
    test.erase(it);

    displayvec(test);

    return 0;
}
void displayvec(std::vector<std::string>&v)
{
    for (std::string s : v )
    {
        std::cout << s << std::endl;
    }
}
Edited by ICanC

Share this post


Link to post
Share on other sites
auto it = std::find(test.begin(), test.end(), "Boat");
test.erase(it);

This works for this particular case, but in general you have to test whether "it" points to a valid element, before trying to erase it.

Share this post


Link to post
Share on other sites

yeah, presumably with the above example by Bregma? So it is valid so long as it doesn't point to the end of the vector

auto it = std::find(test.begin(), test.end(), "Boat");
if (it != test.end())
test.erase(it);
else
std::cout << "No Boat element in vector";

 

Share this post


Link to post
Share on other sites

Indeed.

 

Do yourself a big favor, and always use curly brackets around sub-scopes, like

auto it = std::find(test.begin(), test.end(), "Boat");
if (it != test.end()) {
    test.erase(it);
} else {
    std::cout << "No Boat element in vector";
}

It's technically not needed, but it avoids the trap of adding a second statement, and not realizing it's not part of the sub-scope. That will save you several hours searching.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this