Advertisement

Find and delete Vector element

Started by March 24, 2017 10:22 PM
7 comments, last by Bregma 7 years, 8 months ago

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?

which fails on the std::find line

Fails how?

Hello to all my stalkers.

Advertisement
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.

#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.

Stephen M. Webb
Professional Free Software Developer

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;
    }
}

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.

Advertisement

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";

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.

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

This advice will save your life many times over. You will learn the hard way once.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement