Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 变量-如何赋值_C++_Boost Variant - Fatal编程技术网

C++ 变量-如何赋值

C++ 变量-如何赋值,c++,boost-variant,C++,Boost Variant,我有一个boost变量,我想给它赋值。 代码看起来像 boost::variant <int, std::vector<int>,std::vector<float> > MyVariant; 如何将值赋给,int,int向量和float向量。 简单的作业不起作用。看起来很简单: #include <boost/variant.hpp> #include <vector> int main() { using MyVaria

我有一个boost变量,我想给它赋值。 代码看起来像

boost::variant <int, std::vector<int>,std::vector<float> > MyVariant;
如何将值赋给,int,int向量和float向量。
简单的作业不起作用。

看起来很简单:

#include <boost/variant.hpp>
#include <vector>

int main()
{
    using MyVariant = boost::variant<int, std::vector<int>, std::vector<float>>;
    MyVariant m;
    m = 1;
    m = std::vector<int>{1, 2, 3};
    m = std::vector<float>{1.f, 2.f, 3.f};
    return 0;
}

使用C++17编译器,您可以使用std::variant而不是boost::variant。

请编辑您的问题,以显示您迄今为止的尝试。问题似乎相当清楚。不工作是什么意思?请在问题中包含代码和错误消息。