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 on other sites

which fails on the std::find line

Fails how?

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 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 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 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 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 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 on other sites

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.

Create an account

Register a new account