简体   繁体   English

在C ++中包装std :: iterator

[英]wrap std::iterator in C++

I have a need to wrap a vector iterator, but don't like the idea to rewrite it from scratch. 我需要包装一个矢量迭代器,但不喜欢从头开始重写它的想法。 And I can't subclass it as far as vector iterator doesn't seem to be cross-platform. 而且我不能将它子类化,因为矢量迭代器似乎不是跨平台的。 At least gnu and ibm ones look different. 至少gnu和ibm看起来不同。

What I want to do is the following: 我想做的是以下内容:

class MyContainer {
    vector<double> data;
    vector<int> indices;

    iterator
    begin()
    { return my_iterator(data, indices.begin()); }

    iterator
    end()
    { return my_iterator(data, indices.end()); }
}

MyContainer  cont;

Where indices vector contains integer positions within the data vector. 其中索引向量包含数据向量内的整数位置。 Data is supposed to be much much bigger than the indices. 数据应该比指数大得多。

So I need an iterator that can go through the indices in any direction like a normal vector iterator does with the only exception: it must return a value of data vector when the value is going to be accessed. 所以我需要一个迭代器,它可以在任何方向上遍历索引,就像普通的向量迭代器那样唯一的例外:当要访问该值时,它必须返回数据向量的值。 eg: 例如:

for(MyContainer::iterator it = cont.begin(); it != cont.end(); it++) {
    cout << *it << endl; // values of data should appear here
}

Basically it should look like a normal collection for the std world. 基本上它应该看起来像std世界的正常集合。 You can iterate it in whatever direction you want, you can sort it, run unique, find_if, etc... 你可以在任何你想要的方向迭代它,你可以对它进行排序,运行唯一,find_if等...

any simple solution? 任何简单的方案?

There's a great Boost library for defining custom iterators. 有一个很棒的Boost库用于定义自定义迭代器。 You need to provide a class with a few methods: 您需要提供一些具有几种方法的类:

i.dereference()  Access the value referred to
i.equal(j)       Compare for equality with j
i.increment()    Advance by one position
i.decrement()    Retreat by one position
i.advance(n)     Advance by n positions
i.distance_to(j) Measure the distance to j

Then you get the rest from the iterator_facade . 然后你从iterator_facade得到其余的。

Good luck! 祝好运!

This looks a lot like a permutation_iterator , one of the "built in" adapters from the Boost.Iterator Library 这看起来很像permutation_iterator ,它是Boost.Iterator库中的“内置”适配器之一

See this example (modified from the Boost docs) on codepad. 在键盘上查看此示例 (从Boost文档修改)。

There's nothing in the standard C++ library, but you can probably get boost::iterator_adapter to do what you want. 标准C ++库中没有任何内容,但您可以使用boost::iterator_adapter来执行您想要的操作。 A preliminary examination suggests you'll need to override iterator_adapter::dereference and iterator_adapter::equal . 初步检查表明你需要覆盖iterator_adapter::dereferenceiterator_adapter::equal

template <typename _Scalar=double,
          typename _Idx=int, 
          typename _Seq=std::vector<_Scalar>, 
          typename _IdxVector=std::vector<_Idx> >
class SelIter 
    : public boost::iterator_adaptor< SelIter<_Scalar, _Idx>, 
                                      typename _IdxVector::iterator, _Scalar > 
{
public:
    typedef boost::iterator_adaptor< SelIter, typename _IdxVector::iterator, _Scalar > Base;

    SelIter(_Seq& scalars, _IdxVector& idxs);
    SelIter(_Seq& scalars, typename _IdxVector::iterator pi);

    typename Base::reference dereference() const;
    bool equal(const SelIter& x) const;
private:
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM