#include <iostream>
#include <set>
using namespace std;
// 仿函数, 重载函数调用运算符()的类
class MyFang{
public:
bool operator() (int v1, int v2) {
return v1 > v2;
}
};
void printSetInt(set<int, MyFang> &s){
set<int, MyFang>::const_iterator it = s.begin();
for(; it != s.end(); it++) {
cout << (*it) << " ";
}
cout << endl;
}
void printSetIntOnly(set<int> &s){
set<int>::const_iterator it = s.begin();
for(; it != s.end(); it++) {
cout << (*it) << " ";
}
cout << endl;
}
void test01() {
// set<int, 排序规则> s1; // 默认是从小到大.
set<int, MyFang> s1; // 从大到小排序, 使用仿函数.
s1.insert(30);
s1.insert(10);
s1.insert(40);
s1.insert(4);
s1.insert(21);
printSetInt(s1);
cout << "test01 end ..." << endl;
}
class PersonFang; // 提前声明
class PersonObj{
friend class PersonFang; // 友元类
friend void printSetPersonObj(set<PersonObj, PersonFang> &s); // 友元函数
private:
int num;
string name;
float score;
public:
PersonObj(){};
PersonObj(int num, string name, float score) {
this->num = num;
this->name = name;
this->score = score;
}
};
// 仿函数, 重载函数调用运算符()的类
class PersonFang {
public:
bool operator() (PersonObj v1, PersonObj v2) { // 仿函数
return v1.num > v2.num;
}
};
void printSetPersonObj(set<PersonObj, PersonFang> &s){
set<PersonObj, PersonFang>::const_iterator it = s.begin();
for(; it != s.end(); it++) {
cout << (*it).num << "-" << (*it).name << "-" << (*it).score << " ";
}
cout << endl;
}
void test02() {
set<PersonObj, PersonFang> s1;
s1.insert(PersonObj(108, "luck", 99.2f));
s1.insert(PersonObj(10, "lucka", 919.2f));
s1.insert(PersonObj(1040, "luckb", 929.2f));
s1.insert(PersonObj(1, "luckc", 939.2f));
s1.insert(PersonObj(67, "luckd", 949.2f));
printSetPersonObj(s1);
cout << "test02 end ..." << endl;
}
void test03() {
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(70);
s1.insert(90);
printSetIntOnly(s1);
set<int>::const_iterator ret;
ret = s1.find(50); // 查找元素是否存在,返回该元素的迭代器,无返回尾迭代器.
if (ret != s1.end()) {
cout << "找到的结果为:" << *ret << endl;
}
cout << "元素的个数:" << s1.count(50) << endl;
ret = s1.lower_bound(50);
if (ret != s1.end()) {
cout << "下限为:" << *ret << endl;
}
ret = s1.upper_bound(50);
if(ret != s1.end()) {
cout << "上限为:" << *ret << endl;
}
// 以对组 的方式 存储上下限 pair
pair< set<int>::const_iterator, set<int>::const_iterator > pa;
pa = s1.equal_range(50);
if (pa.first != s1.end()) {
cout << "下限为:" << *(pa.first) << endl;
}
if (pa.second != s1.end()) {
cout << "上限为:" << *(pa.second) << endl;
}
cout << "test03 end ..." << endl;
}
void test04() {
multiset<int> sm; // 可有重复元素的set
sm.insert(10);
sm.insert(20);
sm.insert(10);
sm.insert(30);
multiset<int>::const_iterator it = sm.begin();
for(; it != sm.end(); it++) {
cout << *it <<endl;
}
cout << "test04 end ..." << endl;
}
void test05(){
// 方式一.
pair<int, string> p1(1003, "移动");
pair<int, string> p2(1004, "电信");
pair<int, string> p3(1005, "两");
// 方式二
pair<int, string> p4 = make_pair(9876, "新新");
cout << p4.first << " - " << p4.second << endl;
cout << "test05 end ..." << endl;
}
int main() {
test01();
test02();
test03();
test04();
test05();
return 0;
}
网友评论