-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnested_map.cpp
More file actions
48 lines (37 loc) · 1.03 KB
/
nested_map.cpp
File metadata and controls
48 lines (37 loc) · 1.03 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
//
// Created by Mayank Parasar on 2020-03-23.
//
/*
Here is the definition:
struct nmap;
struct nmap: map<string, boost::variant<string, nmap*>>{};
The last line below doesn't work:
nmap my_map;
my_map["a"] = "b";
my_map["c"] = new nmap;
my_map["c"]["d"] = "e";
*
* */
#include <iostream>
#include <boost/variant.hpp>
#include <map>
using namespace std;
using std::map;
struct nmap;
struct nmap: map<std::string, boost::variant<int, nmap*>>
{
typedef boost::variant<int, nmap*> Variant;
typedef map<int, Variant> base;
friend nmap& as_map(Variant& v) { return *boost::get<nmap*>(v); }
friend nmap const& as_map(Variant const& v) { return *boost::get<nmap*>(v); }
friend int& as_int(Variant& v) { return boost::get<int>(v); }
friend int const& as_int(Variant const& v) { return boost::get<int>(v); }
};
int main()
{
nmap my_map;
my_map["a"] = 1;
my_map["b"] = new nmap;
as_map(my_map["b"])["c"] = 2;
as_map(as_map(my_map["b"])["d"])["e"] = 3;
}