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 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <iostream>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
struct MyPoint
{
MyPoint( unsigned int px = 0, unsigned int py = 0 ) : x(px), y(py) {}
unsigned int x;
unsigned int y;
};
ostream& operator << ( ostream & os, const MyPoint & pt ){ os << "x: " << pt.x << " , y: " << pt.y; return os;}
bool operator < ( const MyPoint & left, const MyPoint & right )
{
if ( left.x < right.x ) return true;
if ( left.x == right.x ) return ( left.y < right.y );
return false;
}
bool operator == ( const MyPoint & right, const MyPoint & left )
{
return ( right.x == left.x && right.y == left.y );
}
typedef pair<MyPoint, unsigned int> PPair;
bool operator < ( const PPair & left, const PPair & right )
{
return ( left.first < right.first );
}
bool operator == ( const PPair & left, const PPair & right )
{
return ( left.first == right.first );
}
ostream& operator << ( ostream & os, const PPair & ppair ){ os << ppair.first << " , value: " << ppair.second << endl; return os;}
typedef map<MyPoint, unsigned int> PMap;
typedef PMap::iterator PMapIt;
void PrintPMap( const PMap & pm, ostream & os = cout ) { copy( pm.begin(), pm.end(), ostream_iterator<PPair>(os)); }
int main(int argc, char** argv){
PMap m1, m2, m3;
m1.insert( PPair( MyPoint( 10, 20 ), 128 ) );
m1.insert( PPair( MyPoint( 15, 10 ), 212 ) );
m1.insert( PPair( MyPoint( 5, 30 ), 124 ) );
m1.insert( PPair( MyPoint( 10, 40 ), 65 ) );
cout << "map1:" << endl;
PrintPMap( m1 );
m2.insert( PPair( MyPoint( 15, 20 ), 89 ) );
m2.insert( PPair( MyPoint( 15, 10 ), 54 ) );
m2.insert( PPair( MyPoint( 5, 30 ), 212 ) );
m2.insert( PPair( MyPoint( 12, 30 ), 165 ) );
cout << endl << "map2:" << endl;
PrintPMap( m2 );
set_intersection( m1.begin(), m1.end(), m2.begin(), m2.end(), std::insert_iterator<PMap>( m3, m3.begin() ) ); //ici ça ne fonctionne pas, aucun élément n'est inséré dans m3
cout << endl << "map3:" << endl;
PrintPMap( m3 );
cout << "end" << endl;
cin.get();
return 0;
} |