这是定义:
struct nmap;
struct nmap: map<string, boost::variant<string, nmap*>>{};
下面的最后一行不起作用:
nmap my_map;
my_map["a"] = "b";
my_map["c"] = new nmap;
my_map["c"]["d"] = "e";
我需要添加什么才能使其正常工作?
答案 0 :(得分:8)
我建议你去找一个小小的可读助手:
#include <boost/variant.hpp>
#include <map>
using std::map;
struct nmap;
struct nmap: map<std::string, boost::variant<std::string, nmap*>>
{
typedef boost::variant<std::string, nmap*> Variant;
typedef map<std::string, 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 std::string& as_string(Variant& v) { return boost::get<std::string>(v); }
friend std::string const& as_string(Variant const& v) { return boost::get<std::string>(v); }
};
int main()
{
nmap my_map;
my_map["a"] = "b";
my_map["c"] = new nmap;
as_map(my_map["c"])["d"] = "e";
}
或者采用递归变体的方式。让我画一下:
这是IMO更优雅:
#include <boost/variant.hpp>
#include <map>
using nmap = boost::make_recursive_variant<std::string, std::map<std::string, boost::recursive_variant_> >::type;
using map_t = std::map<std::string, nmap>;
#include <iostream>
static std::ostream& operator<<(std::ostream& os, nmap const& map);
int main()
{
nmap my_map = map_t
{
{ "a", "b" },
{ "c", map_t
{
{ "d", "e" },
{ "f", map_t
{
{ "most nested", "leaf node" },
{ "empty", map_t { } },
} },
} },
};
std::cout << my_map;
}
乍一看,这可能看起来更复杂,但它实际上有许多重要优势:
nmap
实例,因此有完整的值语义盒子。允许对树的惯用访问,不需要'ifs和 buts dereferences',例如考虑一下我们如何做一个快速的&amp;那个operator<<
:
static std::ostream& operator<<(std::ostream& os, nmap const& map)
{
struct print : boost::static_visitor<void>
{
print(std::ostream& os, int indent = 0) : os(os), indent(indent) { }
void operator()(map_t const& map) const {
os << "{";
for(auto& item : map)
{
os << "\n";
do_indent();
os << " " << item.first << ": ";
boost::apply_visitor(print(os, indent+4), item.second);
}
if (!map.empty()) { os << "\n"; do_indent(); };
os << "}";
}
void operator()(std::string const& str) const {
os << str;
}
private:
std::ostream& os;
void do_indent() const { for (int n = indent; n>0; --n) os << ' '; }
int indent = 0;
};
boost::apply_visitor(print(os), map);
return os;
}
输出:
# g++ -std=c++11 -Wall -pedantic -Wextra main.cpp && ./a.out
{
a: b
c: {
d: e
f: {
empty: {}
most nested: leaf node
}
}
}