map-rbegin

////////////////////////////////////////
//      2018/05/01 16:31:30
//      map-rbegin

// returns a reverse iterator to the end of the map
#include <iostream>
#include <map>
#include <iomanip>
#include <string>

using namespace std;

template<class T>
class ID
{
private:
    T id, name;
public:
    ID(T t, T n) :id(t), name(n){}

    void print(){
        cout.setf(ios::left);
        cout << setw(15) << name << " " << id << endl;
        cout.unsetf(ios::left);
    }

};
//================================================
int main(){
    typedef ID<string> id;
    typedef map<int, id> M;
    typedef M::value_type v_t;

    M m;
    m.insert(v_t(1, id("000123", "Shevchenko")));
    m.insert(v_t(2, id("000124", "Pushkin")));
    m.insert(v_t(3, id("000125", "Shakespeare")));


    // same key
    /*
    因为在 map 中元素的主键是唯一的,当前插入操作将会检测被插入元素的主键是否等于容器中某个
    已存在元素的主键,如果是,新的元素将不会被插入,且返回指向已经存在的元素的迭代器(如果
    当前函数有返回值)。
    */
    std::pair<M::iterator, bool > it;
    it = m.insert(v_t(3, id("000126", "Smith")));
    cout << it.first->first <<  endl;
    it.first->second.print();

    M::reverse_iterator lt = m.rbegin();
    while (lt != m.rend()){
        cout.setf(ios::left);
        cout << setw(3) << lt->first;
        lt->second.print();
        lt++;
    }
    return 0;
}


/*
OUTPUT:
    3
    Shakespeare     000125
    3  Shakespeare     000125
    2  Pushkin         000124
    1  Shevchenko      000123
*/ 
原文地址:https://www.cnblogs.com/laohaozi/p/12537847.html