四种“排序容器” **multiset set multimap map**

STL 中的平衡二叉树
四种“排序容器”
multiset set multimap map

multiset st; T为数据类型
st.insert 插入元素
st.find 查找元素
st.rease 删除元素

代码解释:

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

int main()
{
	multiset<int> st;
	int a[10] = {1, 4, 6, 2, 3, 7, 7, 5, 34, 10 };
	for(int i = 0; i < 10; i++)
		st.insert(a[i]);  // 插入的是 a[i]的复制品
	multiset<int>::iterator i;  // 迭代器 类似于指针  
	for(i = st.begin(); i != st.end(); ++i)
		cout << * i << ",";
		cout << endl; 
	i = st.find(22);  //找到 返回值是迭代器 
	if(i == st.end() )  // 找不到返回值为 end() 
		cout << "not found " << endl;  
	i = st.insert(22);
	if(i == st.end()) 
		cout << "not found " << endl;
	else 
		cout << "found: " << *i << endl; 
		
	i = st.lower_bound(7); // 返回最靠后的迭代器 it 使得 [begin(), it) 中的元素都在 7 前面, 复杂度log(n) 
	cout << *i <<endl; 
	i = st.upper_bound(7);// 返回最靠前的迭代器 it 使得[it, begin(end) ) 中的元素都在 7 的后面
	cout << *i <<endl;
	st.erase(34);
	for(i = st.begin(); i != st.end(); ++i)
		cout << *i <<","; 
 	return 0; 
 } 

multiset 的自定义排序

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


struct Rule1{
	bool operator() (const int & a, const int & b) {
		return (a%10) < (b%10);
		// 返回值为 true 则 a在b的前面 
	}
}; 

struct Student{
	char name[10];
	int id;
	int score;
};

Student students [] = { {"Jack", 112, 78 }, {"Marry", 102, 85}, {"Ala", 333, 92}, {"Xuchen", 101, 70}, {"Shun", 102, 78} };
struct Rule2 {
	bool operator() (const Student & s1,  const Student & s2){
		if(s1.score != s2.score)	return s1.score > s2.score;
		else return (strcmp(s1.name, s1.name) < 0);
	}
};
int main()
{
	multiset<int, greater<int> > st; // 按从大到小排序
	int a[10] = {2, 3, 23, 6, 42, 23, 12, 5, 10, 7 };
	for(int i = 0; i < 10; ++i)
		st.insert(a[i]);  
	multiset<int>::iterator i;
	for(i = st.begin(); i != st.end(); ++i) 
		cout << *i << ",";
		cout << endl;
	multiset<int, Rule1 > st2;
	for(int i = 0; i < 10; ++i)
		st2.insert(a[i]);
	multiset<int>::iterator p;
	for(p = st2.begin(); p != st2.end(); ++p)
		cout << *p << ",";
		cout << endl;
	p = st2.find(182);
	cout << * p << endl;   
	
	multiset<Student, Rule2> st3;
	for(int i = 0; i < 5; ++i){
		st3.insert(students[i]); 
	}
	multiset<Student, Rule2>::iterator q;
	for(q = st3.begin(); q != st3.end(); ++q)
		cout << q->score << " " << q->name << " " << q->id <<endl;
	Student s = {"Marry", 1000, 85};
	q = st3.find(s);
	if(q!= st3.end())
		cout << q->score << " " << q->name << " " << q->id << endl; 
	
	return 0; 
}

greater < T > 从大到小排序


set 和 multiset 的不同在于 set 容器里不能有重复元素!
插入元素可能失败

  • pair 模板
    pair<T1, T2> 类型等价于
    struct{
    T1 first;
    T2 second;
    }
#include<iostream>
#include<cstring>
#include<set>
using namespace std;

int main()
{
	set<int> st;
	int a[10] = {4, 2, 7, 5, 7, 8, 1, 8, 10, 9};
	for(int i = 0; i < 10; ++i)
		st.insert(a[i]);
	cout << st.size() << endl;
	set<int>::iterator i;
	for(i = st.begin(); i != st.end(); ++i)
		cout << * i << " " ;
		cout <<endl; 
	pair<set<int>::iterator, bool> result = st.insert(2);
	if(! result.second)
		cout<< * result.first << " already exists" << endl;
	else 
		cout << * result.first <<" inserted" << endl;
	return 0; 
}


multimap 容器里的元素都是 pair 形式的

struct{
T1 first; // 关键字
T2 second; // 值
}


map 和 multimap的主要区别
~不能有关键字重复的元素
~可以使用 [ ] 下标为关键字 返回值为 first 和关键字相同的元素的 second
~ 插入元素可能失败

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值