类似于一个平衡二叉树的容器multiset和set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//stl-multiset,set(引入头文件<set> #include<iostream> #include<set> #include<cstring> using namespace std; int main(){ multiset<int> st;//先声明一个容器 //st.insert()添加元素; st.find()查找元素,返回值为迭代器,否则为end() //st.erase()删除元素; st.begin()返回第一个元素; st.end()返回最后一个元素 //迭代器可自增自减,!=和==,但不可以比较大小,加减。 int a[10]={1,14,12,13,5,3,13,6,89,7}; for(int i=0;i<10;i++) st.insert(a[i]); multiset<int>::iterator i;//i是一个迭代器,类似于指针 for(i=st.begin();i!=st.end();i++) cout<<*i<<" "; cout<<endl; //类似的multiset也可以自定义排序,在声明时第二个参数位调用 multiset<int,greater<int> > st2; for(int i=0;i<10;i++) st2.insert(a[i]); multiset<int,greater<int> >::iterator j; for(j=st2.begin();j!=st2.end();j++) cout<<*j<<" "; cout<<endl; //set的用法,区别于multiset,set里面的元素不能重复 //这里提一下pair模板的用法(新知识点) //pair <t1,t2> name = struct{ t1 first; t2 second;} name pair <int ,double> x; x.first=1.1;x.second=9; cout<<x.first<<" "<<x.second<<endl; } |