zoukankan      html  css  js  c++  java
  • 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
    */ 
  • 相关阅读:
    定义类或对象
    CSS 超出的文字显示省略号(单行、多行)
    获取Json对象的长度以及判断json对象是否为空
    第三次作业附加
    八皇后问题解题报告(dfs
    STL学习笔记(不定期更新)
    寒假作业之三
    寒假作业之二(2)
    寒假作业之二(1)
    第一篇随笔居然是总结耶
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537847.html
Copyright © 2011-2022 走看看