fork download
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. struct human
  6. {
  7. human( const std::string& name ) : name(name) {}
  8. // ..
  9. const std::string name ;
  10. // ...
  11. bool operator< ( const human& that ) const { return name < that.name ; }
  12. };
  13.  
  14. int main()
  15. {
  16. std::set<human> humans { {"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"g"}, {"h"}, {"i"} } ;
  17. std::set< const human* > selected_hunans ;
  18.  
  19. // select some humans
  20. for( const human& h : humans )
  21. {
  22. static bool select = false ;
  23. if( ( select = !select ) ) selected_hunans.insert( std::addressof(h) ) ;
  24. }
  25.  
  26. std::cout << "\nhumans: " ; for( const human& h : humans ) std::cout << h.name ;
  27. std::cout << "\nselected: " ; for( const human* p : selected_hunans ) std::cout << p->name ;
  28.  
  29. // remove all the currently selected humans from the set of humans
  30. for( const human* p : selected_hunans ) humans.erase(*p) ;
  31. selected_hunans.clear() ; // these pointers are no longer invalid
  32.  
  33. std::cout << "\nremaining humans: " ; for( const human& h : humans ) std::cout << h.name ;
  34. std::cout << '\n' ;
  35. }
  36.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
humans: abcdefghi
selected: acegi
remaining humans: bdfh