結構體用於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