首页 > 代码库 > 结构体用于map,set时要重载运算符<

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

#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;	}}

举例:#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;

 

 

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