boost :: variant - 如何赋值[closed]

问题描述 投票:-5回答:1

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

boost::variant <int, std::vector<int>,std::vector<float> > MyVariant;

如何赋值,int,int的向量和浮点数的向量。简单的分配不起作用。

c++ boost-variant
1个回答
2
投票

看起来很简单:

#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

© www.soinside.com 2019 - 2024. All rights reserved.