1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <string>
#include <list>
#include <algorithm>
#include <memory>
#include <iostream>
struct A
{
std::string id ;
};
bool operator == ( const A& a, const std::string& str ) { return a.id == str ; }
bool operator < ( const A& first, const A& second ) { return first.id < second.id ; }
int main()
{
std::list<A> seq { {"one"}, {"two"}, {"three"}, {"four"}, {"five"} } ;
auto iter= std::find( std::begin(seq), std::end(seq), "three" ) ;
A& alias = *iter ;
A* ptr = std::addressof(alias) ;
const auto dump = [&]
{
for( const auto& s : seq ) std::cout << s.id << ' ' ;
std::cout << '\n' ;
std::cout << iter->id << '/' << alias.id << '/' << ptr->id
<< " at address " << ptr << "\n\n" ;
};
dump() ;
seq.sort() ;
dump() ;
seq.insert( iter, { {"six"}, {"seven"}, {"eight"} } ) ;
dump() ;
seq.erase( std::find( std::begin(seq), std::end(seq), "four" ),
std::find( std::begin(seq), std::end(seq), "seven" ) ) ;
dump() ;
seq.reverse() ;
dump() ;
// iterator/reference/pointer to A{"three"} have remained vaild till now
// everything we have done upto here is a list operation
// a list does not move its elements around in memory
std::cout << "--------------------------\n" ;
std::reverse( std::begin(seq), std::end(seq) ) ;
// iterator/reference/pointer to A{"three"} are now invalidated
// iter no longer 'points' to A{"three"} etc.
// the algorithm does move things around in memory
dump() ;
}
|