比较两个不同对的对象

时间:2012-03-22 03:58:24

标签: c++ stl

此代码是包含出价和数量并询问价格和数量的结构。我们需要能够将出价与要价相匹配,然后在排序后,必须从询问向量中减去出价向量中的数量。我们的问题是如何在数量分类后将数量与价格分开?

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include<iterator>
#include"Classes.h"

using  namespace std;
struct Pss: public pair <double, int>
{
    Pss (double const &s1, int const &s2):
    pair<double, int> (s1,s2)
    {}
};

ostream &operator <<(ostream &out, Pss const &p)
{
    return out<<" "<<p.first<<" "<<p.second<<endl;
}

class Sortby
{
    double Pss::*d_filed;
public:
    Sortby(double Pss::*filed)
        :
    d_filed (filed)
    {}
bool operator () (Pss const &p1, Pss const p2)const 
{
    return p1.*d_filed< p2.*d_filed;
}
};





int main()
{


    double BidP, AskP;
    int BidQ, AskQ;
   vector<Pss> BO;
   vector<Pss> AO;
    cout<< "eneter the Buying Orders, when you are done enter -1 twice"<<endl;
        while(1)
        {
                cin>>BidP>>BidQ;
                if (BidP==-1)
                break;
                BO.push_back(Pss(BidP,BidQ)); 
        }



        sort(BO.begin(),BO.end(),Sortby(&Pss::first));
        cout<<"Bid Orders"<<endl;
        copy(BO.begin(),BO.end(),ostream_iterator<Pss>(cout));



        cout<< "eneter the Selling Orders, when you are done enter -1 twice"<<endl;
        while(1)
        {
                cin>>AskP>>AskQ;
                if (AskP==-1)
                break;
                AO.push_back(Pss(AskP,AskQ));  
        }

        sort(AO.begin(),AO.end(),Sortby(&Pss::first));
        cout<<"Ask Orders"<<endl;
        copy(AO.begin(),AO.end(),ostream_iterator<Pss>(cout));



    int w;
    cin>> w; 

    return 0;
}

0 个答案:

没有答案