fork download
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;


template<typename Iterator>
struct skip_iterator {
  using iterator_category = typename Iterator::iterator_category;
  //using iterator_category = std::input_iterator_tag;
  using value_type = typename Iterator::value_type;
  using difference_type = typename Iterator::difference_type;
  using pointer = typename Iterator::pointer;
  using reference = typename Iterator::reference;
 Iterator _begin, _end;
 std::function<bool(value_type const &)> _predicate;
 using self_t = skip_iterator<Iterator>;

 bool operator==(self_t const & rhs) {
  return (_begin == rhs._begin);
 }
 bool operator!=(self_t const & rhs) {
  return not (*this == rhs);
 }

 reference operator*(void) {
  return *_begin;
 }

 void skip_to_next(void) {
  for(; _begin != _end; ++_begin) {
   if (not _predicate(*_begin)) {
    break;
   }
  }
 }

 skip_iterator (Iterator b, Iterator e,  std::function<bool(value_type const &)> p)
  : _begin (b), _end (e), _predicate (p) {
  skip_to_next ();
 }
 self_t end(void) {
  return {_end, _end, _predicate};
 }
 self_t & operator++(void) {
  skip_to_next();
  return *this;
 }
 difference_type operator-(self_t const & rhs) {
  return _begin - rhs._begin;
 }
 self_t operator+(difference_type inc) {
  return {_begin + inc, _end, _predicate};
 }
 self_t operator-(difference_type inc) {
  return {_begin - inc, _end, _predicate};
 }
 bool operator<(self_t const & rhs) {
  return _begin < rhs._begin;
 }
};

template<typename Iterator, typename Predicate>
auto make_skip_iterator (Iterator f, Iterator t, Predicate p) {
 return skip_iterator<Iterator>{f, t, p};
}

int main() {
 vector<int> input{1, 2, 3, 4, 5, 6, 8, 10, 11, 13};
 auto f = input.begin();
 auto t = input.end();
 auto from = make_skip_iterator (f, t, [](auto e) { return e % 2 == 0; });
 auto to = from.end ();
 sort(from, to, greater<int>{});
 for (auto e : input) {
  cout << e << " ";
 }
 cout << endl;
 return 0;
}
Compilation error #stdin compilation error #stdout 0s 3452KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/5/bits/stl_algo.h:61:0,
                 from /usr/include/c++/5/algorithm:62,
                 from prog.cpp:4:
/usr/include/c++/5/bits/stl_heap.h: In instantiation of 'void std::__sort_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]':
/usr/include/c++/5/bits/stl_algo.h:1929:23:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1943:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Size = int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1963:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = std::greater<int>]'
prog.cpp:76:31:   required from here
/usr/include/c++/5/bits/stl_heap.h:395:4: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
    --__last;
    ^
In file included from /usr/include/c++/5/algorithm:62:0,
                 from prog.cpp:4:
/usr/include/c++/5/bits/stl_algo.h: In instantiation of '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]':
/usr/include/c++/5/bits/stl_algo.h:1918:40:   required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1948:38:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Size = int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1963:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = std::greater<int>]'
prog.cpp:76:31:   required from here
/usr/include/c++/5/bits/stl_algo.h:1899:4: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
    --__last;
    ^
/usr/include/c++/5/bits/stl_algo.h:1901:6: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
      --__last;
      ^
/usr/include/c++/5/bits/stl_algo.h: In instantiation of 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<std::greater<int> >]':
/usr/include/c++/5/bits/stl_algo.h:1850:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1880:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1966:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = std::greater<int>]'
prog.cpp:76:31:   required from here
/usr/include/c++/5/bits/stl_algo.h:1822:7: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
       --__next;
       ^
/usr/include/c++/5/bits/stl_algo.h:1827:4: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
    --__next;
    ^
In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_algobase.h: In instantiation of 'static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _BI2 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]':
/usr/include/c++/5/bits/stl_algobase.h:600:58:   required from '_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _BI2 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]'
/usr/include/c++/5/bits/stl_algobase.h:610:5:   required from '_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _BI2 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]'
/usr/include/c++/5/bits/stl_algobase.h:680:48:   required from '_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _BI2 = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >]'
/usr/include/c++/5/bits/stl_algo.h:1846:8:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1880:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:1966:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<std::greater<int> >]'
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; _Compare = std::greater<int>]'
prog.cpp:76:31:   required from here
/usr/include/c++/5/bits/stl_algobase.h:562:7: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
      *--__result = std::move(*--__last);
       ^
/usr/include/c++/5/bits/stl_algobase.h:562:31: error: no match for 'operator--' (operand type is 'skip_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >')
      *--__result = std::move(*--__last);
                               ^
stdout
Standard output is empty