从istream读取boost :: variant类型

P0W

我正在经历boost::variant,想知道如何进行跟踪工作?

typedef boost::variant<int,std::string> myval;
int main()
{

std::vector<myval> vec;

std::ifstream fin("temp.txt");

//How following can be achieved ?
std::copy(std::istream_iterator<myval>(fin), //Can this be from std::cin too ?
          std::istream_iterator<myval>(),
          std::back_inserter(vec));   
}

对于类数据成员,我们可以选择重载>>运算符,但是该怎么做myval呢?

ro

可以重载operator>>variant,就像任何其他类型。但是,由您决定实现逻辑来决定从流中读取哪种类型并将其存储在中variant这是您可能如何做的完整示例:

#include "boost/variant.hpp"
#include <iostream>
#include <cctype>
#include <vector>
#include <string>

typedef boost::variant<int, std::string> myval;

namespace boost { // must be in boost namespace to be found by ADL
std::istream& operator>>(std::istream& in, myval& v)
{
    in >> std::ws;      // throw away leading whitespace
    int c = in.peek();
    if (c == EOF) return in;  // nothing to read, done

    // read int if there's a minus or a digit
    // TODO: handle the case where minus is not followed by a digit
    // because that's supposed to be an error or read as a string
    if (std::isdigit(static_cast<unsigned char>(c)) || c == '-') {
        int i;
        in >> i;
        v = i;
    } else {
        std::string s;
        in >> s;
        v = s;
    }
    return in;
}
} // namespace boost

// visitor to query the type of value
struct visitor : boost::static_visitor<std::string> {
    std::string operator()(const std::string&) const
    {
        return "string";
    }
    std::string operator()(int) const
    {
        return "int";
    }
};

int main()
{
    std::vector<myval> vec;
    std::copy(
        std::istream_iterator<myval>(std::cin),
        std::istream_iterator<myval>(),
        std::back_inserter(vec));

    std::cout << "Types read:\n";
    for (const auto& v : vec) {
        std::string s = boost::apply_visitor(visitor(), v);
        std::cout << s << '\n';
    }
}

输入示例: 1 2 3 hello 4 world

输出:

Types read:
int
int
int
string
int
string

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从给定的嵌套boost-variant类型创建新的boost-variant类型

通过boost :: mpl获得boost :: variant的类型索引

使用访客无法在Boost Variant中对类型进行分类

如何增加boost :: variant可以处理的类型数量

boost :: variant仅移动类型的处理中的奇怪行为

地图中具有指针类型的Const Boost Variant

如何返回由boost :: varaint返回类型中包含的类型的子集制成的boost :: variant

如何防止将隐式转换为具有多种整数类型的boost :: variant类型?

从istream读取不同类型的列表到vector

(C ++,boost :: variant)boost变量映射的数据类型,并对其执行数学运算

boost :: variant将static_visitor应用于某些类型

当 bool 出现为可能的类型时 boost::variant 给出错误的结果

我应该对带有不同类型参数的类使用Boost Variant吗

boost :: variant具有引用有界类型,无法分配值

在Visual Studio 2010中进行调试时,如何获取boost :: variant变量的隐藏类型?

使用boost :: variant库制作地图。如何以正确的类型存储和显示事物?

使用包含不完整类型的`std :: vector`递归定义和访问`boost :: variant`-libstdc ++ vs libc ++

增量boost :: variant值

Boost-variant的向量

从boost :: variant检索对象

如何从Boost属性树中读取字段类型

比较std :: variant类型实例

如何迭代std :: variant的类型?

Boost :: variant的多态设置器

嵌套Boost :: variant的分段错误

如何读取类型为 std::vector<std::vector<boost:any>> 的 boost 变量?

boost :: variant。boost :: visitor重载功能

std :: variant和boost :: variant之间有什么区别?

std::variant 是否提供类似于 boost::variant<>::types 的功能?