结构体用于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;

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM