结构体用于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;
}
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;
}
相关文章推荐
- 结构体用于map,set时要重载运算符<
- 结构体用于map,set时要重载运算符<
- 结构体作为map的key或放入set中,需要重载<运算符
- 用于把List<Object>转换成Map<String,Object>形式
- C++ map和set中的结构体
- Set<String> java.util.Map.keySet()
- Collections.newSetFromMap(Map<E,Boolean> map)
- 随机获取一个集合(List, Set,Map)中的元素<转>
- 结构体作为map的key或放入set中,需要重载<运算符
- 集合转换java:【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- Java集合三种类型:Set<集>、List<列表>、Map<映射>
- Java集合转换【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- Struts2 JSP中将list,set ,Map传递到Action然后<s:iterator>遍历(三十五)
- Java集合转换【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- set容器中放入结构体时需要重载运算符<
- 结构体作为map的key或放入set中,需要重载<运算符
- set,map用于统计输出
- 使用第三方工具,将JavaBean对象或者List<JavaBean/String/Integer>或Set或Map对象转成JSON
- use map<key, val> in C++, solve LeetCode Anagrams 240 ms pass large test set
- 用于把List<Object>转换成Map<String,Object>形式