• vector


    ////////////////////////////////////////
    //      2018/04/18 7:36:56
    //      vector - rbegin and rend
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    class ID
    {
    private:
        string name;
        int score;
    public:
        friend bool operator < (const ID&, const ID&);
        ID(string name, int score) :name(name), score(score){}
    
        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 vector<ID> Vector;
    
    int main(){
        Vector v;
        Vector::iterator iter;
        v.push_back(ID("Smith A.",96));
        v.push_back(ID("Amstrong B.",91));
        v.push_back(ID("Waston D.",82));
    
        for (iter = v.begin(); iter != v.end(); iter++){
            iter->display();
        }
    
        sort(v.begin(), v.end()); // sort algorithm
        cout << endl << "Sorted by Score" << endl;
        cout << "====================" << endl;
        for (iter = v.begin(); iter != v.end(); iter++){
            iter->display();
        }
    
        cout << endl << "Reverse output" << endl;
        cout << "=================" << endl;
        Vector::reverse_iterator r = v.rbegin();
        while (r != v.rend()){
            r->display();
            r++;
        }
        cout << endl;
    
        return 0;
    }
    
    /*
    OUTPUT:
        96 Smith A.
        91 Amstrong B.
        82 Waston D.
    
        Sorted by Score
        ====================
        82 Waston D.
        91 Amstrong B.
        96 Smith A.
    
        Reverse output
        =================
        96 Smith A.
        91 Amstrong B.
        82 Waston D.
    */ 
  • 相关阅读:
    传输问题
    修改对象目录
    传输与系统单点登录问题
    两个小错误
    BW数据库优化过程记录20100529
    SAP ABAP 到外部数据库Oracle问题
    固定资产传输问题
    软件外包的商业模式和软件人员的职业规划
    做有意义的事,继续研究FarMap
    FarMap诞生了!
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12538031.html
Copyright © 2020-2023  润新知