[英]process a map containing boost::variant<int, double,long long> as value datatype
我有一個地圖數據結構,其中包含一個字符串作為鍵和多個數據類型作為值。 我在實例化地圖時填充了地圖。 我面臨的問題是在遍歷地圖並嘗試訪問每個鍵的值時遇到了一些錯誤。 我認為可能需要將返回值從variant強制轉換為其實際數據類型。 我真的不知道該如何使用它。
這是地圖的定義:map> mapToBeProcessed;
map<string,boost::variant<int,double, long long, string>> mapToBeProcessed;
for(auto &x: mapToBeProcessed)
{
if(ini.hasField(x.first))
{
b << x.first << x.second;
}
}
//
當我嘗試訪問地圖的值時出現問題:x.second
您可以訪問變體,以將功能應用於活動成員。
struct stream_visitor {
using result_type = void;
template <typename T>
void operator()(T& t) { os << name << t; }
std::ostream & os;
std::string name;
}
map<string,boost::variant<int,double, long long, string>> mapToBeProcessed;
for(auto &x: mapToBeProcessed)
{
if(ini.hasField(x.first))
{
boost::apply_visitor(stream_visitor{ b, x.first }, x.second);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.