C ++ iterator over some elements in a vector

Please forgive me if this is a trivial question, I'm just learning C ++ and trying to wrap my head with certain concepts. Especially when it comes to iterators, I am completely lost.

Say I have a custom class that represents some data structure, and one of its members is an integer vector. I want to write a bidirectional iterator for this class that only outputs EVEN numbers in a vector. Is there a simple and instructive way? I would prefer not to use libraries other than STL.

+4
source share
3 answers

#include <vector>
#include <iostream>

class X {

public:

  class EvenIterator {
  public:
    EvenIterator(std::vector<int>::iterator it, std::vector&ltint>::iterator end) : it(it), end(end) {
      while (true) {
        if (isEven(*it)) {
          break;
        } else if (it == end) {
          break;
        }
        it++;
      }
    }

    bool operator != (const EvenIterator& evenIt) {
      return evenIt.it != this->it;
    }

    int operator * () {
      return *it;
    }

    EvenIterator operator ++ () {
      while (true) {
        it++;
        if (isEven(*it)) {
          return EvenIterator(it, end);
        } else if (it == end) {
          return EvenIterator(it, end);
        }
      }
    }
  private:
    std::vector&ltint>::iterator it;    
    std::vector&ltint>::iterator end;    
  };

  static bool isEven(int number) {
    return number % 2 == 0;
  }

  void add(int number) {
    v.push_back(number);
  }

  EvenIterator evenBegin() {
    return EvenIterator(v.begin(), v.end());
  }

  EvenIterator evenEnd() {
    return EvenIterator(v.end(), v.end());
  }

private:
  std::vector&ltint> v;

};

int main() {
  X x;
  x.add(1);
  x.add(2);
  x.add(3);
  x.add(2);
  x.add(2);
  x.add(31);
  x.add(56);
  x.add(101);

  for (X::EvenIterator it = x.evenBegin(); it != x.evenEnd(); ++it){
    std::cout &lt&lt *it &lt&lt std::endl; // only prints the even numbers
  }
}

+1
source

, . , , for_each .

std::for_each . for_each_if . , (4, 6 8).

#include <iostream>
#include <vector>

using namespace std;

struct is_even {
  typedef bool return_type;
  bool operator() (const int& value) {return (value%2)==0; }
};

struct doprint {
  bool operator() (const int& value) { std::cout << value << std::endl; }
};

template <class InputIterator, class Predicate, class Function> 
void for_each_if(InputIterator first, InputIterator last, Function f, Predicate pred) 
{ 
    while ( first != last )
    {  
        if (pred (*first)) 
            f(*first++);  
        else 
            first ++; 
    }
}

int main()
{
   std::vector<int> v;
   v.push_back( 4 );
   v.push_back( 5 );
   v.push_back( 6 );
   v.push_back( 8 );

   for_each_if( v.begin(), v.end(), doprint(), is_even());

   return 0;
}
+3

, vector::iterator, , :

class my_iterator : private std::vector<int>::iterator {
    typedef std::vector<int>::iterator base;
    base m_base_end;   // Stores the real end iterator of the internal vector.
                       // We need it so that the even/odd checking code
                       // doesn't run off the end of the vector
                       // (initialize it in a constructor, omitted here for
                       // brevity).
public:

    using base::operator*;
    using base::operator->;
    // etc. for other members...

    // except for increment:

    my_iterator& operator++()
    {
        do {
            base::operator++();
        } while( static_cast<base&>(*this) != m_base_end
                 && base::operator*() % 2 );
        return *this;
    }

    my_iterator operator++(int)
    {
        my_iterator temp;
        operator++();
        return temp;
    }

    // TODO: decrement and other arithmetic operators
};

, const_iterator ( , , , ).

Boost - filter_iterator. iterator_adaptor, .

+3

All Articles