how do you make a heterogeneous boost::map?

后端 未结 6 2068
旧巷少年郎
旧巷少年郎 2020-11-30 00:15

I want to have a map that has a homogeneous key type but heterogeneous data types.

I want to be able to do something like (pseudo-code):

boost::map&l         


        
6条回答
  •  心在旅途
    2020-11-30 00:34

    Thank you David, that was what I needed. Here's the working solution.

    #include 
    using std::cout;
    using std::endl;
    
    #include 
    #include 
    
    using boost::any_cast;
    typedef std::map t_map;
    
    
    int main(int argc, char **argv)
    {
    
      t_map map;
      char *pc = "boo yeah!";
    
      map["a"] = 2.1;
      map["b"] = pc;
    
      cout << "map contents" << endl;
      cout << any_cast(map["a"]) << endl;
      cout << any_cast(map["b"]) << endl;
    
      return 0;
    }
    

提交回复
热议问题