编写一个迭代器,使多个集装箱看起来为一体 [英] Writing an iterator that makes several containers look as one

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

问题描述

考虑以下简化的例子和所需的输出:

Consider the following simplified example and desired output:

class A
{
    class combined_iterator
    {
        ????
    }
    typedef ??? t_combined_it;

    t_combined_it begin();
    t_combined_it end();

    std::vector<int> m_Vec1, m_Vect2;
}

A a;
a.m_Vec1.push_back(1);
a.m_Vec2.push_back(2);
for (A::t_combined_it it = a.begin() ; it != a.end() ; it++) {
     std::cout << *it << " ";
}

输出:

1 2 

我觉得这个问题是从这一明确的:我怎么写一个迭代器,使得它看起来好像两个或两个以上的其他迭代器是真的只是一个序列。这样一来,在本例中,代替迭代以上两者m_Vec1和m_Vec2,可以使用一个迭代,超过m_Vec1的第一元件,然后m_Vec2迭代,透明

I think the question is clear from this: how do I write an iterator that makes it look as if two or more other iterators are really just one sequence. So that, in the example, instead of iteration over both m_Vec1 and m_Vec2, I can use an iterator that iterates over first the elements of m_Vec1 and then m_Vec2, transparently.

我发现下面的问题,我认为要求是相同的:做一个C ++迭代器遍历 2容器。目前还没有很好的回答这个问题;由原始提问者psented溶液$ P $似乎卷积,并且它是(相对)内存密集型

I found the following question which I think asks the same: Make a c++ iterator that traverses 2 containers . There were no good answers to this question; the solution presented by the original asker seems convoluted, and it is (relatively) memory-intensive.

我尝试了天真的做法通过保持一个std ::矢量::迭代器作为我的自定义迭代器中的一员,它比作.END()每个序列上迭代的迭代器;但是似乎它是非法的,比较来自不同容器的迭代器(在那里我将有preferred他们刚刚返回不等于 - 也许这是一个方向去寻找解决这个问题我不能想到如何实现它,虽然)。

I tried a naive approach by keeping a std::vector::iterator as a member of my custom iterator, and comparing it to the .end() iterators of each of the sequences being iterated over; however it seems that it is illegal to compare iterators from different containers (where I would have preferred them just to return 'not equal' - maybe that is a direction to go for finding the solution to this problem? I can't think of how to implement it, though).

在可能情况下,如果相关的,我想,我在其他地方使用它们使用boost ::迭代器,我喜欢它提供给我的迭代器实现的同质性;但当然,如果有人有不使用他们的想法,我可以在自己工作的他们,所以他们不会在这个意义上必需的。

Where possible and if relevant, I would like to use boost::iterators as I use them elsewhere and I like the homogeneity it provides to my iterator implementations; but of course if someone has an idea without using them, I can work them in myself, so they're not required in that sense.

推荐答案

<一个href=\"http://www.boost.org/doc/libs/1_46_1/libs/range/doc/html/range/reference/utilities/join.html\">boost::join是你在找什么。您也可以学习的实施,特别是如何得出的集装箱穿越,参考和返回值类型的最小公分母。引用:

boost::join is what you're looking for. You can also study the implementation, especially how to derive the lowest common denominator for container traversal, reference and return value types. To quote:

在连接功能的目的是加入两个区域为一个更长的范围。

The intention of the join function is to join two ranges into one longer range.

所得的范围将作为参数提供的两个范围的最低共同穿越。

The resultant range will have the lowest common traversal of the two ranges supplied as parameters.

请注意,该加盟范围导致性能的成本,因为需要检查一个范围的结束>已经在内部遍历期间达成。

Note that the joined range incurs a performance cost due to the need to check if the end of a range > has been reached internally during traversal.

这篇关于编写一个迭代器,使多个集装箱看起来为一体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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