上代码,看效果。
实验前的准备代码:
struct student
{
int id;
char name[30];
};
struct stuless
{
bool operator()(const student& st1,const student& st2)
{
return st1.id<st2.id;
}
};
multiset在main里面的调用,注意 myset.insert(std);调用三次。
int main()
{
student students[3] = {{1,"xiejunjie"},{2,"dengjinff"},{3,"aaab"}};
multiset<student,stuless> myset(students,students+3,stuless());
student std;
std.id = 100;
strcpy(std.name,"abcdefg");
myset.insert(std);//1次
myset.insert(std);//2次
myset.insert(std);//3次
for(multiset<student,stuless>::iterator ib=myset.begin();ib!=myset.end();ib++)
{
cout<<(*ib).id<<" "<<(*ib).name<<endl;
}
}
实验效果显示,std(100,"abcdefg")插入了三次有效,并且没有覆盖。
set用法,在main中的调用方式,注意myset.insert()调用三次。
int main()
{
student students[3] = {{1,"xiejunjie"},{2,"dengjinff"},{3,"aaab"}};
set<student,stuless> myset(students,students+3,stuless());
student std;
std.id = 20;
strcpy(std.name,"zhongguoren");
myset.insert(std);//1次
myset.insert(std);//2次
myset.insert(std);//3次
for(set<student,stuless>::iterator ib = myset.begin();ib!=myset.end();ib++)
{
cout<<(*ib).id<<" "<<(*ib).name<<endl;
}
return 0;
}
实验结果,发现,虽然插入了三次,但是,只有一次有效,也就是说,前面的两次都被覆盖了。