-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmap_get_keys_values(map_to_vectors).cpp
83 lines (65 loc) · 2.26 KB
/
map_get_keys_values(map_to_vectors).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
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
#include <map>
#include <vector>
#include <algorithm> //transform
#include <iostream>
#include <string>
#include <iterator> //ostream_iterator, back_inserter
#define LAMBDA
using namespace std;
struct RetrieveKey
{
template <typename T>
typename T::first_type operator()(T keyValuePair) const
{
return keyValuePair.first;
}
};
struct RetrieveValue
{
template <typename T>
typename T::second_type operator()(T keyValuePair) const
{
return keyValuePair.second;
}
};
//https://stackoverflow.com/questions/110157/how-to-retrieve-all-keys-or-values-from-a-stdmap-and-put-them-into-a-vector
//https://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl
int main()
{
map<int, std::string> mymap;
vector<int> keys;
vector<std::string> values;
mymap[10] = "convert";
mymap[20] = "map";
mymap[30] = "to";
mymap[40] = "vector";
#ifdef LAMBDA
//using lambda
//must be "const" int!
// Retrieve all keys
std::transform(mymap.begin(), mymap.end(), std::back_inserter(keys), [](std::pair<const int, std::string>& p){return p.first;});
// Retrieve all values
std::transform(mymap.begin(), mymap.end(), std::back_inserter(values), [](std::pair<const int, std::string>& p){return p.second;});
// Dump all keys
std::transform(mymap.begin(), mymap.end(), std::ostream_iterator<int>( std::cout, " " ), [](std::pair<const int, std::string>& p){return p.first;});
std::cout << std::endl;
// Dump all values
std::transform(mymap.begin(), mymap.end(), std::ostream_iterator<std::string>( std::cout, " " ), [](std::pair<const int, std::string>& p){return p.second;});
std::cout << std::endl;
#else
// Retrieve all keys
std::transform(mymap.begin(), mymap.end(), std::back_inserter(keys), RetrieveKey());
// Retrieve all values
std::transform(mymap.begin(), mymap.end(), std::back_inserter(values), RetrieveValue());
// Dump all keys
std::copy(keys.begin(), keys.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// Dump all values
std::copy(values.begin(), values.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
#endif
}
/*
10 20 30 40
convert map to vector
*/