-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapEraseNoSmart.cpp
52 lines (40 loc) · 1.04 KB
/
mapEraseNoSmart.cpp
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
// erasing from map
#include <iostream>
#include <map>
#include <memory>
class fooInterface {
public:
virtual int get_x() const;
virtual int get_p(int i);
};
class foo : public virtual fooInterface {
int x;
int* p;
public:
foo(int x):x(x){ p = new int[x]; for(int i=0;i<x;i++) p[i] = i;};
virtual ~foo(){printf("deleting %u\n",x);delete [] p;};
virtual int get_x() const {return x;};
virtual int get_p(int i){return p[i];};
};
struct Cmp {
bool operator()(const foo& lhs, const foo& rhs) const {
return lhs.get_x() < rhs.get_x();
}
};
int main ()
{
std::map<foo, char, Cmp> mymap;
std::map<foo, char, Cmp>::iterator it;
// insert some values:
foo* f = new foo(10);
mymap[std::move(*f)] = 'a';
mymap.emplace(std::move(*(new foo(20))),'b');
foo* t = new foo(20);
printf(" Finds %u\n",mymap.find(*t) != mymap.end());
delete t;
// mymap.erase ( --mymap.end() );
// show content:
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->second << " => " << it->first.get_x() << '\n';
return 0;
}