1 #include <boost/variant.hpp>
 2 #include <boost/any.hpp>
 3 #include <vector>
 4 #include <string>
 5 #include <iostream>
 6 
 7 std::vector<boost::any> vector;
 8 
 9 struct output :
10   public boost::static_visitor<>
11 {
12   void operator()(double &d) const
13   {
14     vector.push_back(d);
15     std::cout << "double: " << d << std::endl;
16   }
17 
18   void operator()(char &c) const
19   {
20     vector.push_back(c);
21    std::cout << "char: " << c << std::endl;
22   }
23 
24   void operator()(std::string &s) const
25   {
26     vector.push_back(s);
27     std::cout << "string: " << s << std::endl;
28   }
29 };
30 
31 int main()
32 {
33   boost::variant<double, char, std::string> v;
34   v = 3.14;
35   boost::apply_visitor(output(), v);
36   v = 'A';
37   boost::apply_visitor(output(), v);
38   v = "Hello, world!";
39   boost::apply_visitor(output(), v);
40 }

 

http://zh.highscore.de/cpp/boost/datastructures.html 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2021-07-21
  • 2021-08-26
猜你喜欢
  • 2022-02-06
  • 2021-12-01
  • 2022-02-19
  • 2021-06-14
  • 2021-09-17
  • 2021-10-18
相关资源
相似解决方案