黑马程序员C++提高9——set/multiset容器

在这里插入图片描述

#include<set>
#include<iostream>
using namespace std;

//输出
void PrintSet(set<int> s) {
    
    
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

//初始化、赋值、插入、删除
void test01() {
    
    

	//初始化和插入
	set<int> s1;		//默认构造
	//默认从小到大排序
	s1.insert(50);
	s1.insert(10);
	s1.insert(30);
	s1.insert(90);
	s1.insert(60);
	s1.insert(20);
	set<int> s2(s1);	//拷贝构造
	cout << "s2:";
	PrintSet(s2);

	//赋值
	set<int> s3;
	s3.insert(5);
	s3.insert(2);
	s3.insert(3);
	set<int> s4;
	s4 = s3;
	cout << "\ns4:";
	PrintSet(s4);
	s4.swap(s2);
	cout << "\n------swap------" << endl;
	cout << "s2:";
	PrintSet(s2);
	cout << "\ns4:";
	PrintSet(s4);

	//大小
	if (s4.empty()) {
    
    
		cout << "\n\ns4为空。" << endl;
	}
	else {
    
    
		cout << "\n\ns4不为空且size=" << s4.size() << endl;
	}

	//删除
	s2.erase(s2.begin());
	cout << "\ns2删除s2.begin()处的元素后:";
	PrintSet(s2);
	s4.erase(50);
	cout << "\ns4删除元素50后:";
	PrintSet(s4);
}

//查找操作
void test02() {
    
    

	set<int> s;
	//默认从小到大排序
	s.insert(50);
	s.insert(10);
	s.insert(30);
	s.insert(90);
	s.insert(60);
	cout << "s: ";
	PrintSet(s);
	//找到指定值key的位置【位置用迭代器表示】
	set<int>::iterator ret = s.find(30);
	if (ret == s.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "指定值ret:" << *ret << endl;
	}

	//找第一个>=key的元素位置
	ret = s.lower_bound(30);
	if (ret == s.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "第一个>=30的元素ret:" << *ret << endl;
	}

	//找到第一个>key的元素位置
	ret = s.upper_bound(30);
	if (ret == s.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "第一个>30的元素ret:" << *ret << endl;
	}
	
	//equal_range是返回lower_bound和upper_bound的值
	pair<set<int>::iterator, set<int>::iterator> myret = s.equal_range(30);
	if (myret.first == s.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "myret.first=" << *(myret.first) << endl;
	}
	if (myret.second == s.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "myret.second=" << *(myret.second) << endl;
	}
}

//自定义类
class Person {
    
    
public:
	Person(int id, int age):id(id), age(age){
    
    }
public:
	int id;
	int age;
};
//仿函数
class mycompare {
    
    
public:
	bool operator()(Person p1, Person p2) const {
    
    
		return p1.age < p2.age;
	}
};
//set采用自定义规则排序
void test03() {
    
    

	set<Person, mycompare> sp;
	Person p1(1, 26), p2(2, 21), p3(3, 19);
	sp.insert(p1);
	sp.insert(p2);
	sp.insert(p3);

	for (set<Person, mycompare>::iterator it = sp.begin(); it != sp.end(); it++)
		cout << "id:" << it->id << "\tage:" << it->age << endl;

	//查找【由于排序时只看了age,查找时也只看age】
	Person p4(4, 26);
	cout << "要查找的id:4	age:26" << endl;
	set<Person, mycompare>::iterator ret = sp.find(p4);
	if (ret == sp.end())
		cout << "没有找到!" << endl;
	else
		cout << "查找到的id:" << ret->id << "\tage:" << ret->age << endl;
	//正常情况下是无法查到p4的,但是由于只看age,所以查到了与p4由相同age的p1
}

//简单测试
int main() {
    
    
	//test01();
	//test02();
	test03();

	cout << endl << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43685399/article/details/108610918
今日推荐