c++ - 迭代 Boost fusion::vector

标签 c++ boost iterator boost-fusion

我正在尝试使用以下方法迭代 boost::fusion vector :

typedef typename fusion::result_of::begin<T>::type t_iter;
  std::cout << distance(begin(t), end(t)) << std::endl;
  for(t_iter it = begin(t); it != end(t); next(it)){
    std::cout<<deref(it)<<std::endl;
  }

distance cout 语句给了我一个有限的长度 (2),但是循环似乎无限期地运行。

非常感谢任何建议!

最佳答案

您不能像那样迭代 Fusion vector ,每个迭代器的类型可能与前一个不同(通常是)。我想这就是为什么您的代码中没有 it = next(it) 的原因,它会产生编译错误。

您可以为此使用 boost::fusion::for_each,以及将每个元素打印到标准输出的函数对象:

struct print
{
    template< typename T >
    void operator()( T& v ) const
    {
        std::cout << v;
    }
};

...

boost::fusion::for_each( t, print() );

关于c++ - 迭代 Boost fusion::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13094535/

相关文章:

c++ - 同步访问分配的内存

c++ - Boost 1.70 io_service 弃用

c++ - 无向图上顶点和边的变化

java - 在 Java(1.5 或更高版本)中,从 Set 中获取(任何)元素的最佳执行方式是什么?

c++ - getline 并用 cout 打印

c++ - boost bimap 的 find() 问题

c++ - 如何将命名函数发送到 boost::phoenix 表达式

c++ - 如何处理 "Cmake Error: Unable to find the requested Boost libraries"?

c++ - 从模板函数返回迭代器到 STL 容器

rust - 如何在 Rust 中正确使用 Iterator::chain