c++ - Improving simplicity with std::list -


what better (cleaner, more readable and/or efficient) ways of doing this:

std::list<fruit*>   apples; std::list<fruit>    basket;  (std::list<fruit*>::iterator niapple(apples.begin());      niapple != apples.end(); niapple++) {      (std::list<fruit>::iterator nibasket(basket.begin());          nibasket != basket.end(); nibasket++) {          if (&(*nibasket) == *niapple) {             basket.erase(nibasket);             break;         }      } // loop  } // loop 

what recommend? primarly need handles apples i'll placing inside basket, remove apples form basket without having search (e.g.. index inside fixed array). however, basket needs allocating , deallocating memory in process.

another c++11 way:

list<fruit*> apples; list<fruit>  basket;  basket.remove_if([](const fruit& fruit) {     return any_of(apples.begin(), apples.end(), [](fruit* apple) {         return &fruit == apple;     }); }); 

now changing first container hold iterators second one:

list<list<fruit>::iterator> apples; list<fruit>  basket;  (auto apple : apples)     basket.erase(apple); 

this way better performance , little or no change interface, iterators behave pointers in cases.

also take @ this: should std::list deprecated?

note both solutions work, basket container must std::list.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -