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