C++ boost::Iterator::Filter Iterator














































C++ boost::Iterator::Filter Iterator



Introduction:-The filter iterator adaptor creates a view of an iterator range in which some elements of the range are skipped. A predicate function object controls which elements are skipped. When the predicate is applied to an element, if it returns true then the element is retained and if it returns false then the element is skipped over. When skipping over elements, it is necessary for the filter adaptor to know when to stop so as to avoid going past the end of the underlying range. A filter iterator is therefore constructed with pair of iterators indicating the range of elements in the unfiltered sequence to be traversed.
Example:- 1. given a range of  numbers in the array and filter them whether they are positive or not using own defined predicate(function or structure) and using make_filter_iterator.
CODE:-
#include <boost/config.hpp> #include <algorithm> #include <functional> #include <iostream> #include <boost/iterator/filter_iterator.hpp> #include <boost/cstdlib.hpp> // for exit_success using namespace std; using namespace boost; struct is_positive_number { bool operator()(int x) { return x>0; } }; struct is_negative_number{ bool operator()(int x){ return x<0; } }; int main() { int num[] = { 0, -1, 4, -3, 5, 8, -2 }; const int n=sizeof(num)/sizeof(int); int* numbers=num; // Example using make_filter_iterator() cout<<"The positive numbers are"<<endl; copy(make_filter_iterator<is_positive_number>(numbers, numbers + n), make_filter_iterator<is_positive_number>(numbers + n, numbers + n), ostream_iterator<int>(cout, " ")); cout<<endl; cout<<"negative numbers are "<<endl; copy(make_filter_iterator<is_negative_number>(numbers, numbers + n), make_filter_iterator<is_negative_number>(numbers + n, numbers + n), ostream_iterator<int>(cout, " ")); }
output:-
The positive numbers are
4 5 8 
negative numbers are 
-1 -3 -2 

Example 2:- Filtering an array of numbers (positive or negative) using  filter_iterator.
Code:-
#include <boost/config.hpp>
#include <algorithm>
#include <functional>
#include <iostream>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/cstdlib.hpp>
using namespace std;
using namespace boost;
struct is_positive_number {
  bool operator()(int x) {
      return   x>0;
    }
};
struct is_negative_number{
    bool operator()(int x){
        return  x<0;
    }
};
int main()
{
  int num[] = { 0, -1, 4, -3, 5, 8, -2 };
  const int N = sizeof(num)/sizeof(int);
  
  int* numbers=num;
 
 // Example using filter_iterator
  typedef filter_iterator<is_positive_number, int*> Ft_pos;
    //for positive numbers
    cout<<"positive numbers are ";
  is_positive_number predicate_pos;
  Ft_pos first_pos(predicate_pos, numbers, numbers + N);
  Ft_pos last_pos(predicate_pos, numbers + N, numbers + N);

  copy(first_pos,last_pos,ostream_iterator<int>(std::cout, " "));
  cout <<endl;
  cout<<"negative numbers are ";
  typedef filter_iterator<is_negative_number,int*>ft_neg;
  is_negative_number predicate_neg;
  ft_neg first_neg(predicate_neg,numbers,numbers+N);
  ft_neg last_neg(predicate_neg,numbers+N,numbers+N);
  copy(first_neg,last_neg,ostream_iterator<int>(cout," "));

}
 Output:-
positive numbers are 4 5 8 
negative numbers are -1 -3 -2 
Conclusion:-Although this looks like very fast but it also has O(n) time complexity but this types of iterators reduces the complexity of the code. This have explained the main concept of this iterator , hope you have learned well.happy learning. 



Comments