#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgoKdGVtcGxhdGU8Y2xhc3MgSXRlciwgY2xhc3MgRj4Kdm9pZCBhcHBseSggSXRlciBiZWdpbiwgSXRlciBlbmQsIEYgZiwgYm9vbCBmb3J3YXJkICkKewogICAgd2hpbGUoIGJlZ2luICE9IGVuZCApIAogICAgICAgIGYoIGZvcndhcmQgPyAqYmVnaW4rKyA6ICotLWVuZCApOwp9CgppbnQgbWFpbigpCnsKICAgIGNvbnN0IGJvb2wgZGVzY2VuZCA9IGZhbHNlOwoKICAgIHN0ZDo6bWFwPGludCwgaW50PiBtYXBwOwogICAgbWFwcFsxXSA9IDE7CiAgICBtYXBwWzJdID0gMjsKICAgIG1hcHBbM10gPSAzOwogICAgbWFwcFs0XSA9IDQ7CgogICAgYXV0byBwcmludCA9IFtdKCBjb25zdCBhdXRvICZwICkgeyBzdGQ6OmNvdXQgPDwgcC5maXJzdCA8PCAiICI7IH07CiAgICBhcHBseSggbWFwcC5iZWdpbigpLCBtYXBwLmVuZCgpLCBwcmludCwgdHJ1ZSApOwogICAgc3RkOjpjb3V0IDw8IHN0ZDo6ZW5kbDsKICAgIGFwcGx5KCBtYXBwLmJlZ2luKCksIG1hcHAuZW5kKCksIHByaW50LCBmYWxzZSApOwogICAgc3RkOjpjb3V0IDw8IHN0ZDo6ZW5kbDsKfQoK