Iterating through a list and searching for each element in another list in c++ -
Iterating through a list and searching for each element in another list in c++ -
i have created list of 4 elements called "worklist." have list called "choicelist" has around 40 elements in it. want check if of elements in "worklist" nowadays in "choicelist".
i understand i'd utilize "std::find" search, how go checking each element in order "worklist" in "choicelist"?
baring in mind 1 time element "worklist" has been found in "choicelist" want search process end (and preferably kind of notification element in "worklist" first 1 match).
you have iterate on elements of first list , check there in sec list using find function. if finds searching element , print , break loop.
std::list<int> worklist; std::list<int> choicelist; //this loop works in c++11 , above, //in case of c++98, need utilize iterators iterate on list for( auto &x : worklist) { //auto feature of c++11 , above auto y = std::find (std::begin(choicelist), std::end(choicelist), x); if( y != choicelist.end()) { std::cout<< *y<<"\n"; break; } }
if need find first element of first container in sec container,
auto y= std::find_first_of(std:begin(choicelist), std::end(choicelist), std::begin(worklist), std::end(worklist)); if( y != choicelist.end()) { std::cout<< *y<<"\n"; }
c++ list search iterator elements
Comments
Post a Comment