deque-rbegin and rend

////////////////////////////////////////
//      2018/04/25 7:20:33
//      deque-rbegin and rend

#include <iostream>
#include <iomanip>
#include <deque>
#include <string>
#include <iterator>
#include <algorithm>

using namespace std;

class ID{
private:
    string name;
    int score;
public:
    ID(string name, int score) :name(name), score(score){};
    friend bool operator <(const ID &, const ID &);
    void display(){
        cout.setf(ios::left);
        cout << setw(3) << score << name << endl;
    }
};

//------------------------------------
// comperation function for sorting
bool operator < (const ID& a, const ID& b){
    return a.score < b.score;
}

typedef deque<ID> Deque;
int main(){
    Deque d;
    Deque::iterator iter;
    d.push_back(ID("Smith A", 96));
    d.push_back(ID("Amstrong B.", 91));
    d.push_back(ID("Watson D", 82));
    for (iter = d.begin(); iter != d.end(); iter++){
        iter->display();
    }

    sort(d.begin(), d.end());

    cout << endl << "Sorted by Score " << endl;
    cout << "==================" << endl;
    for (iter = d.begin(); iter != d.end(); iter++){
        iter->display();
    }

    cout << endl << "Reverse output" << endl;
    cout << "==================" << endl;

    Deque::reverse_iterator r = d.rbegin();
    while (r != d.rend()){
        (r++)->display();
    }
    cout << endl;
    return 0;
}


/*
OUTPUT:
    96 Smith A
    91 Amstrong B.
    82 Watson D

    Sorted by Score
    ==================
    82 Watson D
    91 Amstrong B.
    96 Smith A

    Reverse output
    ==================
    96 Smith A
    91 Amstrong B.
    82 Watson D
*/ 
posted @ 2018-04-25 07:46  老耗子  阅读(78)  评论(0编辑  收藏  举报