#include <iostream>

#include <iostream>
#include <map>

template<class Iter, class F>
void apply( Iter begin, Iter end, F f, bool forward )
{
    while( begin != end ) 
        f( forward ? *begin++ : *--end );
}

int main()
{
    const bool descend = false;

    std::map<int, int> mapp;
    mapp[1] = 1;
    mapp[2] = 2;
    mapp[3] = 3;
    mapp[4] = 4;

    auto print = []( const auto &p ) { std::cout << p.first << " "; };
    apply( mapp.begin(), mapp.end(), print, true );
    std::cout << std::endl;
    apply( mapp.begin(), mapp.end(), print, false );
    std::cout << std::endl;
}

