-
how to search a list
how do you perform a simple search on a list?
is this the correct code?
list<string> master;
string target1;
where = seek<string> (master.begin(), master.end(), target1);
it gives me an error saying that i don't have WHERE defined. but i thought by assigning it, it took care of that.
-
you always need to define a variable before assigning it or otherwise using it
-
what would i assign it as then? an iterator?
-
Code:
list<string> lst;
string str_to_find("AStringToFind");
// Initialize the list with some items here
// Find whatever you are looking for here
list<string>::iterator iter = find( lst.begin(), lst.end(), str_to_find);
// do whatever operation you wanted to do
if ( iter == lst.end() ) {
cout<<"Not found"<<endl;
}
else {
// whatever you want to do: I remove the element
lst.erase(iter);
}
-
the code makes sense to me and thank you for that.
but it keeps saying that FIND is an undefined variable?
i thought find was a reserved word?? can anyone help?
-
i tried SEARCH as well...and same thing.
is there something aside from the code that you gave me that i need. i have included list and cstdlib.
??
-
I´m not sure but try
Code:
#include <algorithm>