您的位置:首页 > 编程语言 > C语言/C++

结构体用于map,set时要重载运算符<

2015-03-16 14:23 267 查看
[c-sharp] view
plaincopy

#include<iostream>  

#include<set>  

using namespace std;  

struct P  

{  

    int entry;  

    int time;  

    bool operator<(const P  &b)const {  

        return (this->entry<b.entry);  

    }  

      

};  

int main()  

{  

    while(!cin.eof())  

    {  

        int n;  

        cin>>n;  

        set<P> s;  

        P tmp;  

          

        for(int i = 0;i<n;i++)  

        {  

            tmp.time = 1;  

            cin>>tmp.entry;  

            if(s.find(tmp)==s.end())s.insert(tmp);  

            else  

            {  

                set<P>::iterator it;  

                  

                it = s.find(tmp);  

                tmp=*it;  

                tmp.time++;  

                s.erase(it);  

                s.insert(tmp);  

            }  

        }  

        set<P>::iterator itr;  

        for(itr = s.begin();itr!=s.end()&&!cin.eof();itr++)  

            if(itr->time % 2)  

                cout<<itr->entry<<endl;  

    }  

}  

 

[c-sharp] view
plaincopy

#include<iostream>  

#include<set>  

#include<map>  

#include<string>  

using namespace std;  

int main()  

{  

    int n;  

    while(cin>>n)  

    {  

        map<int,int> nmap;  

        map<int,int>::iterator itr;  

        for(int i = 0;i<n;i++)  

        {  

            int key;  

            cin>>key;  

            itr = nmap.find(key);  

            if(itr==nmap.end())  

                nmap.insert(pair<int,int>(key,1));  

            else  

            {  

                nmap[key] = itr->second+1;  

            }  

        }  

        for(itr = nmap.begin();itr!=nmap.end();itr++)  

            if(itr->second % 2)  

                cout<<itr->first<<endl;  

    }  

}  

 

一道水题,引出了大问题:

当在STL应用中,我们常常会使用到结构体,这就需要我们对特定要求的运算符进行重载。例如在,STL中的排序都是默认使用小于号来排序。因此,在对结构体排序时,我们就需要重载小于号!

 

 

举例:

#include <map>

#include <iostream>

#include <string>

using namespace std;

//学生信息

typedef struct tagStudentInfo

{

 int nID;

 string strName;
 bool operator <(const tagStudentInfo &A) const

 {

  if (nID < A.nID) return true;  //先比较nID

  if (nID == A.nID) return strName.compare(A.strName) < 0;   //nID相同时,再比较strName

  return false;

 }

}StudentInfo,*pstudentInfo;

int main()

{

 //用学生信息映射分数

 map<StudentInfo,int> mapStudent;

 StudentInfo studentInfo;

 studentInfo.nID = 1;

 studentInfo.strName = "student_one";

 mapStudent.insert(map<StudentInfo,int>::value_type(studentInfo,90));

 studentInfo.nID = 2;

 studentInfo.strName = "student_two";

 mapStudent.insert(map<StudentInfo,int>::value_type(studentInfo,80));

 studentInfo.nID = 2;

 studentInfo.strName = "student_three";

 mapStudent.insert(map<StudentInfo,int>::value_type(studentInfo,80));

 map<StudentInfo,int>::iterator iter;

 for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)

  cout << iter->first.nID << iter->first.strName << endl << iter->second << endl;

 return 0;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ stl