list<OBJECT*>::iterator it;
for(it=lstList.begin();it!=lstList.end();++it)
{
if((*it)->test=true)
//do stuff
}
list<OBJECT*>::iterator it=lstList.begin();
while(it!=lstList.end())
{
if((*it)->test=true)
//do stuff
++it
}
list<OBJECT*>::iterator it;
for(it=lstList.begin();it!=lstList.end();++it)
{
if((*it)->test=true)
//do stuff
}
list<OBJECT*>::iterator it=lstList.begin();
while(it!=lstList.end())
{
if((*it)->test=true)
//do stuff
++it
}
#include <list>#include <algorithm>bool test_pred(const OBJECT* const obj) { return obj->test; }std::list<OBJECT*>::iterator it;it = std::find_if( lstList.begin(), lstList.end(), test_pred);
#include <list>#include <algorithm>#include <functional>struct test_pred : std::unary_function<OBJECT*, bool>{ bool operator()(const OBJECT* const obj) const { return obj->test; }};std::list<OBJECT*>::iterator it;it = std::find_if( lstList.begin(), lstList.end(), test_pred());
#include <list>#include <algorithm>#include <boost/mem_fn.hpp>std::list<OBJECT*>::iterator it;it = std::find_if( lstList.begin(), lstList.end(), boost::mem_fn(&OBJECT::test) );
Quote:Original post by utilae
Let's say I have a list of objects (struct that contains a bool test). I want to go through the list and find an object where the bool test=true. What's the fastest way to do this.
Is is this method:
*** Source Snippet Removed ***
This method:
*** Source Snippet Removed ***
Or some other method. I read somewhere about something called std::find, though I don't know how to use it.
if((*it)->test=true)
But rather if((*it)->test == true)
struct check_test ( bool operator() (const TOBJECT* obj) const { return obj->test == true; }};list<OBJECT*>::iterator iter = std::find_if(lstList.begin(),lstList.end(),check_test());if (iter != lstList.end()) // do stuff
Quote:Original post by snk_kidI suppose you could reduce the number of comparsions by applying a binary search. But that hardly makes the search logarithmic.
You can use a sorted std::list with STL algorithms lower_bound, upper_bound, equal_range, binary_search, to obtain logarithmic time but you need to have meaning-full sort & search criteria.
Quote:Original post by doynaxQuote:Original post by snk_kidI suppose you could reduce the number of comparsions by applying a binary search. But that hardly makes the search logarithmic.
You can use a sorted std::list with STL algorithms lower_bound, upper_bound, equal_range, binary_search, to obtain logarithmic time but you need to have meaning-full sort & search criteria.