#include <set>
#include <string>
#include <iostream>
struct human
{
human( const std::string& name ) : name(name) {}
// ..
const std::string name ;
// ...
bool operator< ( const human& that ) const { return name < that.name ; }
};
int main()
{
std::set<human> humans { {"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"g"}, {"h"}, {"i"} } ;
std::set< const human* > selected_hunans ;
// select some humans
for( const human& h : humans )
{
static bool select = false ;
if( ( select = !select ) ) selected_hunans.insert( std::addressof(h) ) ;
}
std::cout << "\nhumans: " ; for( const human& h : humans ) std::cout << h.name ;
std::cout << "\nselected: " ; for( const human* p : selected_hunans ) std::cout << p->name ;
// remove all the currently selected humans from the set of humans
for( const human* p : selected_hunans ) humans.erase(*p) ;
selected_hunans.clear() ; // these pointers are no longer invalid
std::cout << "\nremaining humans: " ; for( const human& h : humans ) std::cout << h.name ;
std::cout << '\n' ;
}
I2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RydWN0IGh1bWFuCnsKICAgIGh1bWFuKCBjb25zdCBzdGQ6OnN0cmluZyYgbmFtZSApIDogbmFtZShuYW1lKSB7fQogICAgLy8gLi4KICAgIGNvbnN0IHN0ZDo6c3RyaW5nIG5hbWUgOwogICAgLy8gLi4uCiAgICBib29sIG9wZXJhdG9yPCAoIGNvbnN0IGh1bWFuJiB0aGF0ICkgY29uc3QgeyByZXR1cm4gbmFtZSA8IHRoYXQubmFtZSA7IH0KfTsKCmludCBtYWluKCkKewogICAgc3RkOjpzZXQ8aHVtYW4+IGh1bWFucyB7IHsiYSJ9LCB7ImIifSwgeyJjIn0sIHsiZCJ9LCB7ImUifSwgeyJmIn0sIHsiZyJ9LCB7ImgifSwgeyJpIn0gfSA7CiAgICBzdGQ6OnNldDwgY29uc3QgaHVtYW4qID4gc2VsZWN0ZWRfaHVuYW5zIDsKCiAgICAvLyBzZWxlY3Qgc29tZSBodW1hbnMKICAgIGZvciggY29uc3QgaHVtYW4mIGggOiBod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