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 <iostream>
#include <map>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef pair<char, int> key;
typedef multimap<key, string> bar;
struct FindFirstKey
{
bool operator()(const pair<key, string>& lhs, const char& rhs) const
{
return (lhs.first.first < rhs);
}
bool operator()(const char& lhs, const pair<key, string>& rhs) const
{
return (lhs < rhs.first.first);
}
};
int main()
{
const bar b = {
{key(5,20), "aaa"},
{key(100,20), "bbb"},
{key(5,10), "ccc"},
{key(5,20), "ddd"}
};
printf("--- all ---\n");
for (const auto& v : b)
{
printf("%03d:%03d\t%s\n", v.first.first, v.first.second, v.second.c_str());
}
printf("--- find(key) ---\n");
{
const auto bound = b.equal_range(key(5,20));
for (auto it = bound.first; it != bound.second; ++it)
{
printf("%03d:%03d\t%s\n", it->first.first, it->first.second, it->second.c_str());
}
}
printf("--- find(key.first) ---\n");
{
const auto bound = equal_range(b.begin(), b.end(), 5, FindFirstKey());
for (auto it = bound.first; it != bound.second; ++it)
{
printf("%03d:%03d\t%s\n", it->first.first, it->first.second, it->second.c_str());
}
}
return 0;
}
|