链迭代器 [英] chain iterator for C++

查看:122
本文介绍了链迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的itertools工具实现了迭代器,它基本上将许多不同的迭代器合并成一个单一。

Python's itertools tools implements a chain iterator which essentially merges a number of different iterators into a single one.

C ++中有类似的东西吗?快速浏览boost库没有发现类似的东西,这是非常令人惊讶的我。难以实现此功能?

Is there something similar in C++ ? A quick look at the boost libraries didn't reveal something similar, which is quite surprising for me. Is it difficult to implement this functionality ?

推荐答案

遇到类似问题时,遇到此问题。

Came across this question while investigating for a similar problem.

即使问题是老的,现在在C ++ 11和boost 1.54的时候,使用 Boost.Range 库。它具有 加入 - functions ,它可以将两个范围合并成一个范围。在这里,您可能会遇到性能损失,这是最低的共同范围概念(即单次通过范围或前进范围等)用作新范围的类别,并且在迭代期间可以检查迭代器是否需要跳转到新范围,但是您的代码可以容易地写为: p>

Even if the question is old, now in the time of C++ 11 and boost 1.54 it is pretty easy to do using the Boost.Range library. It features a join-function, which can join two ranges into a single one. Here you might incur performance penalties, as the lowest common range concept (i.e. Single Pass Range or Forward Range etc.) is used as new range's category and during the iteration the iterator might be checked if it needs to jump over to the new range, but your code can be easily written like:

#include <boost/range/join.hpp>

#include <iostream>
#include <vector>
#include <deque>

int main()
{
  std::deque<int> deq = {0,1,2,3,4};  
  std::vector<int> vec = {5,6,7,8,9};  

  for(auto i : boost::join(deq,vec))
    std::cout << "i is: " << i << std::endl;

  return 0;
}

这篇关于链迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆