include iostream include map include string struct end_init_t end_init

 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
#include <iostream>
#include <map>
#include <string>
struct end_init_t { };
end_init_t end_init;
template <typename Key, typename Value, typename Compare = std::less<Key>,
typename Allocator = std::allocator<std::pair<const Key, Value> > >
class map_initializer
{
public:
typedef typename std::map<Key, Value, Compare, Allocator> map_t;
map_initializer() { }
map_initializer &operator,(const std::pair<Key, Value> &pair)
{
_map.insert(pair);
return *this;
}
map_t operator,(end_init_t)
{
return _map;
}
private:
map_t _map;
};
const std::map<std::string, std::string> mymap = (
map_initializer<std::string, std::string>(),
std::make_pair("fruit", "apple"),
std::make_pair("language", "c++"),
end_init);
int main()
{
std::cout << mymap.find("fruit")->second << std::endl;
}