-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys_values.cpp
More file actions
145 lines (118 loc) · 4.02 KB
/
keys_values.cpp
File metadata and controls
145 lines (118 loc) · 4.02 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <functional>
template<class Pair, class First>
struct select1st : std::unary_function<Pair, First>
{
First operator() (Pair pair) const
{
return pair.first;
}
};
template<class Pair, class Second>
struct select2nd : std::unary_function<Pair, Second>
{
Second operator() (Pair pair) const
{
return pair.second;
}
};
template<class Container>
struct Types {
typedef typename Container::value_type value_type;
typedef typename value_type::first_type first_type;
typedef typename value_type::second_type second_type;
typedef select1st<const value_type&, const first_type&> first_const_selector;
typedef select1st<value_type&, first_type&> first_selector;
typedef select2nd<const value_type&, const second_type&> second_const_selector;
typedef select2nd<value_type&, second_type&> second_selector;
typedef boost::transform_iterator<
first_selector, typename Container::iterator
> first_iterator;
typedef boost::transform_iterator<
first_const_selector, typename Container::const_iterator
> first_const_iterator;
typedef boost::transform_iterator<
second_selector, typename Container::iterator
> second_iterator;
typedef boost::transform_iterator<
second_const_selector, typename Container::const_iterator
> second_const_iterator;
typedef boost::iterator_range<first_iterator> first_range;
typedef boost::iterator_range<first_const_iterator> first_const_range;
typedef boost::iterator_range<second_iterator> second_range;
typedef boost::iterator_range<second_const_iterator> second_const_range;
};
template<class Container>
typename Types<Container>::first_const_range keys(const Container& container)
{
typedef Types<Container> Types;
return typename Types::first_const_range(
typename Types::first_const_iterator(
container.begin(), typename Types::first_const_selector()
),
typename Types::first_const_iterator(
container.end(), typename Types::first_const_selector()
));
}
template<class Container>
typename Types<Container>::first_range keys(Container& container)
{
typedef Types<Container> Types;
return typename Types::first_range(
typename Types::first_iterator(
container.begin(), typename Types::first_selector()
),
typename Types::first_iterator(
container.end(), typename Types::first_selector()
));
}
template<class Container>
typename Types<Container>::second_const_range values(const Container& container)
{
typedef Types<Container> Types;
return typename Types::second_const_range(
typename Types::second_const_iterator(
container.begin(), typename Types::second_const_selector()
),
typename Types::second_const_iterator(
container.end(), typename Types::second_const_selector()
));
}
template<class Container>
typename Types<Container>::second_range values(Container& container)
{
typedef Types<Container> Types;
return typename Types::second_range(
typename Types::second_iterator(
container.begin(), typename Types::second_selector()
),
typename Types::second_iterator(
container.end(), typename Types::second_selector()
));
}
#include <boost/foreach.hpp>
#include <iostream>
#include <map>
#include <algorithm>
namespace {
int main_()
{
typedef std::map<int, double> Map;
Map map;
map[1] = 5.0; map[3] = 3.0;
BOOST_FOREACH(int x, keys(map)) {
std::cout << x << " ";
}
std::cout << std::endl;
BOOST_FOREACH(double& x, values(map)) {
x += 1.0;
}
BOOST_FOREACH(double x, values(const_cast<const Map&>(map))) {
std::cout << x << " ";
}
std::cout << std::endl;
std::cout << *std::max_element(values(map).begin(), values(map).end()) << std::endl;
return 0;
}
}