简体   繁体   中英

How to create an iterator over elements that match a derived type in C++?

I'd like an iterator in C++ that can only iterate over elements of a specific type. In the following example, I want to iterate only on elements that are SubType instances.

vector<Type*> the_vector;
the_vector.push_back(new Type(1));
the_vector.push_back(new SubType(2)); //SubType derives from Type
the_vector.push_back(new Type(3));
the_vector.push_back(new SubType(4)); 

vector<Type*>::iterator the_iterator; //***This line needs to change***

the_iterator = the_vector.begin();
while( the_iterator != the_vector.end() ) {
    SubType* item = (SubType*)*the_iterator;
    //only SubType(2) and SubType(4) should be in this loop.
    ++the_iterator;
}

How would I create this iterator in C++?

You must use a dynamic cast.

the_iterator = the_vector.begin();
while( the_iterator != the_vector.end() ) {
    SubType* item = dynamic_cast<SubType*>(*the_iterator);
    if( item != 0 )
       ... 

    //only SubType(2) and SubType(4) should be in this loop.
    ++the_iterator;
}

Solution without boost. But if you have an access to the boost library - use Filter Iterator as was proposed.

template <typename TCollection, typename T>
class Iterator
{
public:
    typedef typename TCollection::iterator iterator;
    typedef typename TCollection::value_type value_type;

    Iterator(const TCollection& collection,
             iterator it):
        collection_(collection),
        it_(it)
    {
        moveToNextAppropriatePosition(it_);
    }
    bool operator != ( const Iterator& rhs )
    {
        return rhs.it_ != it_;
    }
    Iterator& operator++()
    {
        ++it_;
        moveToNextAppropriatePosition(it_);
        return *this;
    }
    Iterator& operator++(int);
    Iterator& operator--();
    Iterator& operator--(int);
    value_type& operator*()
    {
        return *it_;
    }
    value_type* operator->()
    {
        return &it_;
    }
private:
    const TCollection& collection_;
    iterator it_;
    void moveToNextAppropriatePosition(iterator& it)
    {
        while ( dynamic_cast<T*>(*it) == NULL && it != collection_.end() ) 
            ++it;
    }
};

class A
{
public:
    A(){}
    virtual ~A(){}
    virtual void action()
    {
        std::cout << "A";
    }
};
class B: public A
{
public:
    virtual void action()
    {
        std::cout << "B";
    }
};
int main()
{
    typedef std::vector< A* > Collection;
    Collection c;
    c.push_back( new A );
    c.push_back( new B );
    c.push_back( new A );

    typedef Iterator<Collection, B> CollectionIterator;
    CollectionIterator begin(c, c.begin());
    CollectionIterator end(c, c.end());

    std::for_each( begin, end, std::mem_fun(&A::action) );
}

As paintballbob said in a comment, you should create your own iterator class, perhaps inheriting from vector<Type*>::iterator . In particular, you will need to implement or override operator++() and operator++(int) to ensure that you skip non-SubType objects (you can use dynamic_cast<SubType*>() to check each item). There is a nice overview of implementing your own container and iterator in this O'Reilly Net article .

Just another way how to do it using boost iterators. This time, using std::remove_copy_if :

std::remove_copy_if(v.begin(), v.end(), 
    boost::make_function_output_iterator(boost::bind(&someFunction, _1)),
    !boost::lambda::ll_dynamic_cast<SubType*>(boost::lambda::_1));

It will call a function (In this example someFunction . But it can be anything boost::bind can construct - also a member-function) for each pointer that's pointing to a SubType .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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