multiset与set是STL中的排序容器
multiset<T> st;
st.insert
添加元素,st.find
查找元素,st.erase
删除元素,复杂度#include
#include
#include //使用multiset和set需要此头文件
using namespace std;
int main()
{
multiset<int> st;
int a[10]={1,14,12,13,7,13,21,19,8,8 };
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;
/*输出:1,7,8,8,12,13,13,14,19,21,*/
i = st.find(22); //查找22,返回值是迭代器
if( i == st.end()) //找不到则返回值为 end()
cout << "not found" << endl;
st.insert(22); //插入 22
i = st.find(22);
if( i == st.end())
cout << "not found" << endl;
else
cout << "found:" << *i <<endl;
//找到则返回指向找到的元素的迭代器
/*输出:
not found
found:22 */
i = st.lower_bound(13);
//返回最靠后的迭代器 it,使得[begin(),it)中的元素
//都在 13 前面 ,复杂度 log(n)
cout << * i << endl;
i = st.upper_bound(8);
//返回最靠前的迭代器 it,使得[it,end())中的元素
//都在 8 后面,复杂度 log(n)
cout << * i << endl; //1,7,8,8,12,13,13,14,19,21,
st.erase(i); //删除迭代器 i 指向的元素,即12
for(i = st.begin(); i != st.end(); ++i)
cout << * i << ",";
return 0;
}
/*输出:
13
12
1,7,8,8,13,13,14,19,21,22,*/
multiset<T>::iterator p;
st.begin()
返回一个迭代器,是指向st中的头一个元素的迭代器st.end()
返回一个迭代器,是指向st中的最后一个元素的迭代器#include
#include
#include
using namespace std;
struct Rule1 {
bool operator()( const int & a,const int & b)
{
return (a%10) < (b%10);
}//返回值为true则说明a必须排在b前面
};
int main() {
multiset<int,greater<int> > st; //排序规则为从大到小
int a[10]={1,14,12,13,7,13,21,19,8,8 };
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 << ",";
cout << endl;
/*
输出: 45
21,19,14,13,13,12,8,8,7,1,
*/
multiset<int,Rule1 > st2;
//st2的元素排序规则为:个位数小的排前面
for(int i = 0;i < 10; ++i)
st2.insert(a[i]);
multiset<int,Rule1>::iterator p;
for(p = st2.begin(); p != st2.end(); ++p)
cout << * p << ",";
cout << endl;
p = st2.find(133);
cout << * p << endl;
return 0;
}
/*
输出:
1,21,12,13,13,14,7,8,8,19,
13
*/
#include
#include
#include
#include
using namespace std;
struct Student {
char name[20];
int id;
int score;
};
Student students [] = { {"Jack",112,78},{"Mary",102,85},
{"Ala",333,92},{"Zero",101,70},{"Cindy",102,78}};
struct Rule {
bool operator() (const Student & s1,const Student & s2)
{
if( s1.score != s2.score) return s1.score > s2.score;
else return (strcmp(s1.name,s2.name) < 0);
}
};
int main()
{
multiset<Student,Rule> st;
for(int i = 0;i < 5;++i)
st.insert(students[i]); //插入的是students[i]的复制品
multiset<Student,Rule>::iterator p;
for(p = st.begin(); p != st.end(); ++p)
cout << p->score <<" "<<p->name<<" "
<< p->id <<endl;
Student s = { "Mary",1000,85};
p = st.find(s);
if( p!= st.end())
cout << p->score <<" "<< p->name<<" "
<< p->id <<endl;
return 0;
}
/*
92 Ala 333
85 Mary 102
78 Cindy 102
78 Jack 112
70 Zero 101
85 Mary 102
*/
#include
#include
#include
using namespace std;
int main()
{
set<int> st;
int a[10]={1,2,3,8,7,7,5,6,8,12};
for(int i=0;i<10;i++)
st.insert(a[i]);
cout<<st.size()<<endl; //输出8
set<int>::iterator i;
for(i=st.begin();i!=st.end();i++)
cout<<*i<<","; //输出:1,2,3,5,6,7,8,12,
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;
}
/*
输出:
2 already exists.
*/
pair
struct{
T1 first;
T2 second;
};
例如:pair
等价于:
struct {
int first;
double second;
} a;
a.first = 1;
a.second = 93.93;