# include<iostream>
# include<algorithm>
# include<set>
# include<string>
using namespace std;
#if 0
set/multiset容器会对插入的元素自动排序,以红黑树为底层机制
不能通过迭代器修改容器内的值
set容器不允许有重复元素
multiset允许有重复元素
# endif
void test1()
{//构造、赋值、大小操作
set<int> st;
st.insert(5);
st.insert(7);
st.insert(6);
st.insert(2);
st.insert(1);
st.insert(3);
st.insert(3);
if(!st.empty())
cout << "set中元素的数量:" << st.size() << endl;
for(set<int>::iterator it = st.begin();it != st.end();++it)
cout << *it << " ";
cout << endl;
set<int> st2 = st;
cout << "重载等号赋值:" << endl;
for(set<int>::iterator it = st2.begin();it != st2.end();++it)
cout << *it << " ";
cout << endl;
//st.swap(st2);//交换操作
}
void test2()
{//插入、删除操作
set<int> st;
st.insert(5);
st.insert(7);
st.insert(6);
st.insert(2);
st.insert(1);
st.insert(3);
cout << "遍历:"<< endl;
set<int>::iterator it;
for(it = st.begin();it != st.end();++it)
cout << *it << " ";
cout << endl;
st.erase(3);//删除元素值
cout << "删除元素值为3的:" << endl;
for(it = st.begin();it != st.end();++it)
cout << *it << " ";
cout << endl;
st.insert(3);
it = st.begin();
it++;it++;
st.erase(it);
cout << "删除第3个元素:" << endl;
for(it = st.begin();it != st.end();++it)
cout << *it << " ";
cout << endl;
set<int> st2 = st;
st.clear();//clear操作
if(st.empty())
cout << "clear操作:" << st.size() << endl;
st2.erase(st2.begin(),st2.end());//erase(beg,end)操作
if(st2.empty())
cout << "erase(beg,end):" <<st.size() << endl;
}
#if 0
pair对组,将两个值合成一个值,通过first和second迭代器访问
template<classT1,classT2>
构造1:
pair<string,int> pair("name",1)
pair.first = "name";
pair.second = 1;
构造2:
pair<string,int> pair = make_pair("name",1);
# endif
void test3()
{//查找
set<int> st;
st.insert(5);
st.insert(7);
st.insert(6);
st.insert(2);
st.insert(1);
st.insert(3);
cout << "find用法:" << endl;
set<int>::iterator ret;
ret = st.find(3);
cout << *ret << endl;
ret = st.find(10);//若是没找到,返回set.end()迭代器
cout << "*st.find(10):" << *ret << endl;
cout << "*st.end():"<< *st.end() << endl;
cout << "lower_bound(elem)/upper_bound(elem)返回第一个key <=/> elem的元素的迭代器" << endl;
ret = st.lower_bound(1.5);
cout << *ret << " <= 1.5 <= ";
ret = st.upper_bound(1.5);
cout << *ret << endl;
cout << "equal_range的用法:" << endl;
pair<set<int>::iterator,set<int>::iterator> bound = st.equal_range(1.5);
cout << *bound.first << " <= 1.5 <= " << *bound.second << endl;
}
class compare{
public:
bool operator()(int a,int b){
return a > b;
}//仿函数
};
void test4()
{//改变默认的插入顺序
set<int,compare> st;
st.insert(5);
st.insert(7);
st.insert(6);
st.insert(2);
st.insert(1);
st.insert(3);
for(set<int>::iterator it = st.begin();it != st.end();++it)
cout << *it << " ";
cout << endl;
}
class Person{
public:
Person(string Name,int Id):name(Name),id(Id){}
public:
string name;
int id;
};
class Com{
public:
bool operator()(Person p1,Person p2){//这个地方不能用引用
return p1.id > p2.id;
}
};
void test5()
{//存储对象
Person p1("A",5),p2("B",2),p3("C",4),p4("D",1),p5("E",3);
set<Person,Com> st;
st.insert(p1);
st.insert(p2);
st.insert(p3);
st.insert(p4);
st.insert(p5);
for(set<Person,Com>::iterator it = st.begin();it != st.end();++it)
cout << "name:" << (*it).name << " " << "id:" << (*it).id << endl;
/*
此时,插入和查找的关键字都变成Person的id ,即以id为唯一索引
*/
cout << "--------" << endl;
cout << "查找p6('R',5):" << endl;
Person p6("R",5);
set<Person,Com>::iterator ret = st.find(p6);
cout << "name:" << (*ret).name << " " << "id:" << (*ret).id << endl;
}
int main()
{
//test1();//构造、赋值、大小操作
//test2();//插入删除
//test3();//查找操作
//test4();//修改默认的插入顺序
test5();//set存储对象
return 0;
}
运行结果:
test1:
test2:
test3:
test4:
test5: