#include<iostream>
#include<algorithm>
#include <vector>
#include <fstream>
#include <string>
#include <set>
using namespace std;
class person
{
char name[20];
int id;
public:
person(char*na){strcpy(name,na);}
const char* Getname()const {return name;}
bool operator<(person &ob)const;
};
class ban
{
set<person> A;
public:
void save();
};
bool person::operator <( person &ob )const
{
if(id<ob.id)return true;
else return false;
}
void ban::save()
{
set<person>::iterator i=A.begin();
cout<<i->Getname();
}
int main()
{
return 0;
}
set每次新增元素就会进行排序,如果数据结构比较复杂,则需要自定义一个判断排列顺序的函数,不然程序会报错
这一步必不可少:
bool person::operator <( person &ob )const
{
if(id<ob.id)return true;
else return false;
}
下面再给出结构体使用set容器的方法:
#include<iostream>
#include <set>
using namespace std;
struct man
{
double b;
char a;
int c;
bool operator<(const man &B)const;
};
bool man::operator<(const man &B)const
{
if(b>B.b)return true;
else return false;
}
int main()
{
set<man>A;
man B;
B.a=1;
B.b=2;
B.c=3;
A.insert(B);
man C;
C.a=1;
C.b=2;
C.c=3;
A.insert(C);
return 0;
}