Re: [讨论] boost::variant有没有更好的做法?

楼主: Killercat (杀人猫™)   2017-09-06 13:43:29
自己回自己,之前原来是自己在CMakefile搞了一个大乌龙
所以本来该working的code被误以为会吐出一狗票错误
顺便写一下这篇来感叹boost 赞叹boost一下,这真的还挺神奇的
Code都很短也可验证 所以就不特地贴gist了
前提 :
typedef boost::variant<std::string, double, int> ValueType;
std::map<std::string, ValueType> map;
map里面塞进一些资料
map.insert(std::make_pair("StringValue", "this is string!"));
map.insert(std::make_pair("intValue", 12345));
对map做serialize
std::ofstream ofs(folder);
boost::archive::text_oarchive ar(ofs);
ar << map;
ofs.close();
对map做deserialize
std::map<std::string, ValueType> map2;
std::ifstream ifs(folder);
boost::archive::text_iarchive ar(ifs);
ar >> map2;
ifs.close();
这样就可以了,也就是说,其实他基本上可以直接吃内含variant的map不会出错
之前出错是因为linking flag设错了 =o=
只能说连这东西都吃得下去,感恩boost,赞叹boost......
作者: damody (天亮damody)   2017-09-06 17:28:00
神奇
作者: descent (“雄辩是银,沉默是金”)   2017-09-06 17:33:00
好威
作者: troylee (troylee)   2017-09-10 21:55:00
C++11 make_pair 可以改用 initializer list {a,b}

Links booklink

Contact Us: admin [ a t ] ucptt.com