#include <iostream>
#include <functional>
#include <cctype>
#include <algorithm>
#include <iterator>

template <typename InputIterator>
class IteratorWithSkip : public std::iterator<std::forward_iterator_tag, typename InputIterator::value_type>
{
public:
    typedef typename InputIterator::value_type value_type;
    typedef bool (*skip_function)(value_type);
    
    explicit IteratorWithSkip(InputIterator begin, InputIterator end, skip_function f) 
        : m_final(false),  m_it(begin), m_end(end), m_f(f) {}
        
    IteratorWithSkip() : m_final(true) {}
    
    IteratorWithSkip(const IteratorWithSkip& it) : m_it(it.m_it), m_end(it.m_end), m_final(it.m_final), m_f(it.m_f) {
        skip();
    }
    
    IteratorWithSkip& operator=(const IteratorWithSkip& it) {
        if (this != &it) {
            m_it = it.m_it;
            m_end = it.m_end;
            m_final = it.m_final;
            m_f = it.m_f;
        }
        return *this;
    }
    
    IteratorWithSkip operator++(int) { 
        IteratorWithSkip tmp(m_it, m_end, m_f);
        ++(*this);
        return tmp;
    }
    
    IteratorWithSkip& operator++(){
        if (m_it != m_end) ++m_it;
        skip();
        return *this;
    }
    
    bool operator==(const IteratorWithSkip& it) const {
        return (it.m_final && m_it == m_end);
    }
    
    bool operator!=(const IteratorWithSkip& it) const {
        return ! (*this == it);
    }
    
    value_type operator*() {
        return *m_it;
    }
private:
    void skip() {
        while (m_it != m_end && m_f(*m_it)) ++m_it;
    }
    bool m_final;
    InputIterator m_it, m_end;
    skip_function m_f;
};

bool is_space (char s)
{
    return isspace(s);
}

int main()
{
    std::string s1 = "hello world", s2 = " hellow o r l d   ";
    typedef IteratorWithSkip<std::string::iterator> SkipIterator;
    
    SkipIterator i1(s1.begin(), s1.end(), is_space), i1_end, i2(s2.begin(), s2.end(), is_space);
    
    std::copy (SkipIterator(s2.begin(), s2.end(), is_space), 
               SkipIterator(), 
               std::ostream_iterator<char>(std::cout)); 
               
    std::cout << std::endl;

    std::cout << std::boolalpha << std::equal (i1, i1_end, i2) << std::endl;
}