Is there a find()
function for list as there was in vector?
Is there a way to do that in list?
You use std::find from
, which works equally well for std::list
and std::vector
. std::vector
does not have its own search/find function.
#include
#include
int main()
{
std::list ilist;
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(3);
std::list::iterator findIter = std::find(ilist.begin(), ilist.end(), 1);
}
Note that this works for built-in types like int
as well as standard library types like std::string
by default because they have operator==
provided for them. If you are using using std::find
on a container of a user-defined type, you should overload operator==
to allow std::find
to work properly: EqualityComparable concept