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 72 73 74 75 76
| struct STest
{
DWORD _dwRef;
int* _pi;
STest(DWORD dwRef=0, int* pi=NULL):
_dwRef(dwRef), _pi(pi)
{
cout << "constructeur" << endl;
}
STest& operator= (const STest& T)
{
cout << "operator=" << endl;
if(this != &T)
{
this->_dwRef = T._dwRef;
this->_pi = T._pi;
}
return *this;
}
STest(const STest& T)
{
cout << "copie" << endl;
this->_dwRef = T._dwRef;
this->_pi = T._pi;
}
};
std::map<int, STest> M;
void f(int key, const STest& T)
{
std::map<int, STest>::iterator It = M.find(key);
if (It != M.end())
{
// Clé déjà présente : on réaffecte
It->second = T;
}
else
{
// Clé inexistante : on insère
M.insert(std::make_pair(key, T));
}
//M[key] = T;
}
int main()
{
int x = 1;
int y = 2;
int z = 3;
cout << "Construction des 3 objets ..." << endl << endl;
STest T1(1001, &x);
STest T2(1002, &y);
STest T3(1003, &z);
cout << endl << endl;
cout << "insertion1 ..." << endl << endl;
f(x, T1);
cout << "insertion2 ..." << endl << endl;
f(y, T2);
cout << "insertion3 ..." << endl << endl;
f(x, T3);
system("pause"); |