Vectors Issue!!

Hi, I'm writing a program where I have to use a vector to store the name of visitors to a hospital. The program will let you view the list of visitors, add a name, check to see if a name is on the list, and remove a name from the list. I'm having trouble figuring out how to check if a name is on the list, and how to remove a name from the list. This is my first time using vectors and am finding it extremely difficult. If I comment out these two sections of codes, where choice ==3, and choice == 4, I can view the list and add to it! If anyone could help me out that would be great! Here is the code:

Last edited on
You are going to want to use std::find instead of std::search

http://www.cplusplus.com/reference/algorithm/find/
http://www.cplusplus.com/reference/algorithm/search/

Also in line 41 find and search both return iterators so you don't need to dereference find_name
Same for find_name in line 42.

Line 53 you can't assign an iterator from a value like that. Use std::find.

Line 54 you don't need to dereference delete_names, you are using the assignment operator instead we need to use the not equal to operator, and you want to compare it to names.end().

Line 55 You don't need to dereference delete_name since erase takes an iterator argument.

You also need to get input for name for choice 4

Hopefully that helped fix some of your problems
Hmm, that didn't seem to work, I keep getting a crazy error that is taking up the whole VIM screen when I compile. I'm gonna see if I can figure it out! Thanks for the articles though, they were helpful!
I usually search a vector like this:

1
2
auto myIter = std::find(myVec.begin(),myVec.end(),myData);
bool bFound = myIter != myVec.end();


i remove items in a similar way:

1
2
3
4
auto myIter = std::find(myVec.begin(),myVec.end(),myData);
bool bFound = myIter != myVec.end();
if (bFound)
   myVec.erase(myIter);

Last edited on
Topic archived. No new replies allowed.