std :: vector或其他数据结构中不同类型的对象

Ant*_*kin 0 c++ stl

我想存储这样的结构

  template <typename var_type> struct
  {
  int some_var;
  //...some antoher vars
  var_type problem_var;
  };
Run Code Online (Sandbox Code Playgroud)

在矢量或类似矢量的东西,我该怎么做?我无法使用模板内容创建矢量

MSa*_*ers 5

如果你知道前面的类型列表,boost::variant<>可能比比较合适boost::any.在您的情况下,您可能需要

typedef boost:variant<
   mystruct<float>,
   mystruct<int>
   mystruct<std::string>
> my_variant;
Run Code Online (Sandbox Code Playgroud)

这将只允许这3种类型.或者,您可能想要mystruct<boost:variant<float, int, std::string> >,表面上看似相似.我无法从你的问题中判断哪一个最适合你的问题.