平衡二叉树
有时候需要在添加删除数据的同时还需要大量的数据查找。复杂度:log(n)
排序➕二分查找:不可以
multiset
定义:multiset<类型>变量名
自动维持有序
——排序规则:a<b,a在b前(基本简单的规则)
头文件#include< set >;
eg.nultiset< int >st
st.insert()插入
st.find()查找
st.erase()删除
迭代器
multiset< int > st;
multiset< int>::iterator i ;
st.begin() 返回值类型是multiset< int >::iterator 指向st第一个元素
st.end() 返回值类型是multiset< int >::iterator 指向st最后一个元素后面的位置
++ :上一个
–:下一个
迭代器类似于指针,指向multiset中的元素。可++ – != == 但是不同的:指针可以比大小,加减,multiset 上的不可。(其他有的可)
i = st.lower_bound(x);
返回最靠后的迭代器,使从[st.begin(),i)中的元素,都在x前面
i = st.lower_bound(x);
返回最靠后的迭代器,使从[i,st.end())中的元素,都在x后面
#include <iostream>
#include <set>
#include<algorithm>
using namespace std;
int main() {
multiset<int> st;
int a[10] = {1,2,3,3,4,5,6,7,8,8};
for(int i=0; i<10; i++) st.insert(a[i]);
multiset<int>::iterator i;
i = st.upper_bound(3);
cout << *i << endl; // 4
i = st.lower_bound(3);
cout << *i << endl; // 3
i = st.upper_bound(5);
cout << *i << endl; // 6
i = st.lower_bound(5);
cout << *i << endl; // 5
st.erase(i);
for(i=st.begin(); i!=st.end(); i++) cout << *i << " "; // 1 2 3 3 4 6 7 8 8
i = st.upper_bound(900);
cout << *i << endl; // 2
return 0;
}
排序规则
默认:从小到大
从大到小:greater< int >
#include <iostream>
#include <set>
#include<algorithm>
using namespace std;
int main() {
multiset<int,greater<int>> st;
int a[10] = {9,5,6,2,3,5,6,7,2,1};
for(int i=0; i<10; i++) {
st.insert(a[i]);
}
multiset<int,greater<int>>::iterator i;
for(i=st.begin(); i!=st.end() ; i++) {
cout << *i << " ";
}
//9 7 6 6 5 5 3 2 2 1
return 0;
}
自定义规则:
自己理解:为什么要放到结构体中:因为set类型要求
#include <iostream>
#include <set>
#include<algorithm>
using namespace std;
//自定义规则:比较最后一位数字
struct rule{
bool operator()(const int &a,const int &b){
return a%10 < b%10;
}
}; //最后的;不要忘
int main() {
multiset<int,rule> st;
int a[10] = {91,52,64,26,35,53,61,73,20,19};
for(int i=0; i<10; i++) {
st.insert(a[i]);
}
multiset<int,rule>::iterator i;
for(i=st.begin(); i!=st.end(); i++) cout << *i << " ";
cout << endl;
//20 91 61 52 53 73 64 35 26 19
i = st.find(133);
cout << *i << endl;
//53
return 0;
}
⚠️注意:find(x)不是简单的找到一个y==x,而是在自己给定的规则下,x必须排在y前面和y必须排在y前面都不成立
自定义结构体排序:
#include <iostream>
#include <set>
#include<string>
#include<algorithm>
using namespace std;
//自定义规则:比较最后一位数字
struct stu{
string name;
int grade;
};
struct rule{
bool operator()(stu a,stu b){
return a.grade>b.grade;
}
};
int main() {
multiset<stu,rule>st;
stu s[5]={{"amy",98},{"bob",67},{"kiki",59},{"lili",81},{"andy",12}};
for(int i=0; i<5; i++) st.insert(s[i]);
multiset<stu,rule>::iterator i;
for(i=st.begin(); i!=st.end(); i++) cout << i->name << " " << i->grade << endl;
/*
amy 98
lili 81
bob 67
kiki 59
andy 12
*/
stu s2 = {"celece",100};
st.insert(s2);
for(i=st.begin(); i!=st.end(); i++) cout << i->name << " " << i->grade << endl;
/*
celece 100
amy 98
lili 81
bob 67
kiki 59
andy 12
*/
return 0;
}
set
- set与multiset不同之处:容器里不能有重复元素。
重复的含义:a必须排在b前面和b必须排在a前面 都不成立 - set插入元素可能不成功(不能重复)
#include <iostream>
#include <set>
#include<algorithm>
using namespace std;
int main() {
set<int,greater<int>> st;
int a[10] = {1,1,2,2,3,3,4,4,5,5};
for(int i=0; i<10; i++) {
st.insert(a[i]);
}
set<int,greater<int>>::iterator i;
cout << st.size() << endl;
//5
for(i=st.begin(); i!=st.end(); i++) cout << *i << " ";
//5 4 3 2 1
return 0;
}
前置知识:pair
数据类型:pair类
pair<T1,T2>xx 等价于
struct xx{
T1 first;
T2 second;
};
- 定义:
pair<数据类型1,数据类型2>名称
pair<int,int> a;
pair<int,double> b;
pair<Node,deoN> c;//两个结构体
a.first:第一个元素
a.second:第二个元素
- 插入:
eg: pair<int,int>a;
a=make_pair(4,5);
#include <iostream>
#include<string>
using namespace std;
int main() {
pair<string,string>p;
p=make_pair("a","b");
cout <<p.first << endl; //a
cout <<p.second << endl; //b
return 0;
}
set的用法:
#include <iostream>
#include<string>
#include<set>
using namespace std;
int main() {
set<int>st;
for(int i=0; i<10; i++) st.insert(i);
pair<set<int>::iterator,bool> result = st.insert(2);
if(!result.second) //条件成立:说明插入不成功
cout << *result.first << "already sxists." << endl;
else
cout << *result.first << "inserted." << endl;
//2already sxists.
return 0;
}