1.set容器是一种关联式容器,在插入数据时会根据键值进行排序,不允许出现重复的键值。
2.set容器不能通过迭代器来修改键值,其迭代器不支持随机访问.
3.multiset允许键值重复
#include<iostream>
#include<set> //set 或 multiset
#include<string>
using namespace std;
//关联式容器:在插入数据时会按照键值大小顺序对元素进行排序,排序后的序列为最终的容器元素系列
//集合set特性:
//1.所有元素都会根据元素的键值自动被排序,set容器不允许出现的键值.
//2.set容器不可以通过迭代器来改变元素的值,因为如果改变了值,那么就不一定是有序的系列,破坏了set容器的组织,也就是说set的迭代器是一种const_iterator
//3.set的迭代器不支持随机访问,set的键值(key)就是实值(value),只能递增++或递减--
void printSet(set<int> s) {
for (set<int>::iterator it(s.begin()); it != s.end(); ++it)
//if(it!=s.end()-1) //错误,不支持随机访问
cout << *it << " ";
cout << endl;
}
//初始化和赋值
//初始化插入数据使用emplace或者insert
void test01() {
set<int> s1;
s1.insert(20);
s1.emplace(10);
s1.emplace(-100);
s1.insert(90);
printSet(s1);
set<int> s2(s1); //拷贝构造
printSet(s2);
set<int> s3;
s3 = s2; //重载=赋值
printSet(s3);
}
//大小和交换
void test02() {
set<int> s1;
s1.emplace(-2);
s1.emplace(100);
s1.emplace(30);
if (!s1.empty()) { //判断是否为空
cout << "大小:" << s1.size() << endl; //返回容器大小
cout << "最大大小:" << s1.max_size() << endl;
}
else cout << "set为空" << endl;
set<int> s2;
s2.emplace(4000);
s2.swap(s1); //交换两个set对象的信息
printSet(s2);
printSet(s1);
}
//插入和删除
void test03() {
set<int> s1;
//1.插入数据
s1.insert(1000);
s1.emplace(200);
s1.emplace(30);
s1.emplace(300000);
printSet(s1);
//2.删除数据
s1.erase(30); //① 删除指定的值为30的元素
printSet(s1);
s1.erase(s1.begin()); //② 删除指定迭代器位置的元素
printSet(s1);
s1.erase(s1.begin(), --s1.end()); // ③ 删除左闭右开的区间内的元素
printSet(s1);
s1.clear(); //清空
if (s1.empty())
cout << "空集合" << endl;
else printSet(s1);
}
//查找
void test04() {
set<int> s1;
s1.emplace(20);
s1.emplace(300);
//find(elem) 找到elem则返回对应的迭代器位置,没找到返回终止迭代器end()
set<int>::iterator it(s1.find(300));
if (it != s1.end())
cout << "找到" << *it << endl;
else cout << "待查找元素不存在" << endl;
it = s1.find(9999);
if (it != s1.end())
cout << "找到" << *it << endl;
else cout << "待查找元素不存在" << endl;
}
//统计个数
void test05() {
set<int> s1;
s1.emplace(20);
s1.emplace(300);
s1.emplace(300);
s1.emplace(10);
//count(elem)返回元素个数
int num(s1.count(300)); //1.在set容器里,只有0或1
cout << "在set中,300的个数为: " << num << endl;
multiset<int> s2;
s2.insert(100);
s2.emplace(100);
s2.emplace(20);
num = s2.count(100); //2.在multiset里允许重复值存在
cout << "在multiset中,100的个数为: " << num << endl;
}
//对组的使用
//作用:用于存放成对出现的数据,两个数据的数据类型可以相同和不相同
void test06() {
//第一种创建方式
pair<string, int> p1 = make_pair("李白", 25);
//获取对组里的数据
cout << "姓名:" << p1.first << "\t年龄:" << p1.second << endl;
//第二种创建方式
pair<int, int> p(1, 2000000);
cout << "编号:" << p.first << "\t工资:" << p.second << endl;
}
//语言类型系统自身内置数据类型指定排序规则:使用函数对象实现
class MyCompare {
public:
//利用仿函数设置排序规则为降序
bool operator()(int a, int b) {
return a > b ? true : false;
}
};
void test07() {
set<int,MyCompare> s1;
s1.insert(1000);
s1.emplace(200);
s1.emplace(30);
s1.emplace(300000);
for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); ++it)
cout << *it << " ";
cout << endl;
}
//自定义数据类型,必须指定排序规则
class Person {
public:
Person(string n, int a) :name_(n), age_(a) {}
public:
string name_;
int age_;
};
class PersonCompare {
public:
bool operator()(const Person& p1, const Person& p2) {
return p1.age_ < p2.age_; //按照年龄递增排序
}
};
void test08() {
Person p1("小昭", 18);
Person p2("周芷若", 20);
Person p3("赵敏", 16);
set<Person, PersonCompare> s;
s.emplace(p1);
s.emplace(p2);
s.emplace(p3);
for (set<Person, PersonCompare>::iterator it = s.begin(); it != s.end(); ++it)
cout << "姓名:" << it->name_ << "\t年龄:" << (*it).age_ << endl;
}
int main() {
/*cout << "*************初始化和赋值**********" << endl;
test01();*/
/*cout << "*************大小和交换**********" << endl;
test02();*/
/*cout << "*************插入和删除**********" << endl;
test03();*/
/*cout << "*************查找**********" << endl;
test04();*/
/*cout << "*************统计个数**********" << endl;
test05();*/
/*cout << "*************对组**********" << endl;
test06();*/
/*cout << "*************类型系统内置数据类型指定排序规则**********" << endl;
test07();*/
cout << "*************自定义数据类型指定排序规则**********" << endl;
test08();
system("pause");
return 0;
}