4.1 关联式容器,内部插入数据时候自动排序
4.2 set容器 不允许插入重复的key值
4.3 删除 erase(key)
4.4 find 查找 查找容器中是否有响应的key的数据,如果有返回迭代器,没有返回end的位置
4.5 count 统计 统计key的个数,对于set而言,结果要么是0,要么是1
4.6 lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
4.7 upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
4.8 equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
4.8.1 返回值是一个 对组 pair< 第一个元素,第二个元素 >
4.9 对组声明方式
4.9.1 pair<string, int> p(string("Tom"), 18);
4.9.2 pair<string, int> p2 = make_pair("Jerry", 20);
4.9.3 第一个元素 first 第二个元素 second
4.10 set容器只能插入不重复key值
4.10.1 insert返回值是 pair< iterator , bool >
4.10.2 bool代表了插入是否成功
4.11 对于set容器的排序
4.11.1 必须要在插入之前 指定出排序规则
4.11.2 配合仿函数 实现
4.11.3 自定义数据类型 ,必须要指定出排序规则
set构造函数
set<T> st;
mulitset<T> mst;
set(const set &st);
set赋值操作
set& operator=(const set &st);
swap(st);
set大小操作
size();
empty();
set插入和删除操作
insert(elem);
clear();
erase(pos);
erase(beg, end);
erase(elem);
void test01() {
set<int>s;
s.insert(45);
s.insert(69);
s.insert(87);
s.insert(23);
s.insert(34);
s.insert(45);
printSet(s);
if (s.empty()) {
cout << "空" << endl;
}
else {
cout << "不空" << endl;
}
s.erase(s.begin());
printSet(s);
s.erase(34);
printSet(s);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/08e1b8202e1440b38c98b404a6659fa6.png)
set查找操作
find(key);
count(key);
lower_bound(keyElem);
upper_bound(keyElem);
equal_range(keyElem);
void test02() {
set<int>s;
s.insert(10);
s.insert(30);
s.insert(20);
s.insert(50);
s.insert(40);
set<int>::iterator pos = s.find(30);
if (pos != s.end()) {
cout << "找到了元素:" << *pos << endl;
}
else {
cout << "找不到元素:" << endl;
}
int num = s.count(10);
cout << "10的个数为:" << num << endl;
set<int>::iterator res = s.lower_bound(30);
if (res != s.end()) {
cout << "找到lower_bound的值为:" << *res << endl;
}
else {
cout << "未找到" << endl;
}
res = s.upper_bound(30);
if (res != s.end()) {
cout << "找到upper_bound的值为:" << *res << endl;
}
else {
cout << "未找到" << endl;
}
pair<set<int>::iterator, set<int>::iterator>it = s.equal_range(30);
if (it.first != s.end()) {
cout << "找到equal_range中的 lower_bound的值为: " << *(it.first) << endl;
}
else {
cout << "未找到" << endl;
}
if (it.second != s.end())
{
cout << "找到equal_range中的 upper_bound的值为: " << *(it.second) << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/4e126608d1704b909869386d1d552b98.png)
void test03() {
set<string>s;
pair<set<string>::iterator,bool> p=s.insert("姜山");
if (p.second ) {
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
pair<set<string>::iterator, bool> p2 = s.insert("姜山");
if (p2.second) {
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/d8b06706950d46cf92922db0187bbca2.png)
set容器插入自定义数据类型
class MyCompare {
public:
bool operator()(int v1,int v2) {
return v1 > v2;
}
};
void test04() {
set<int, MyCompare>s1;
for (int i = 0; i < 7; ++i) {
s1.insert(rand() % 100);
}
for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/1df9793c703e4bf8b414b4f4c3a7f4be.png)
class Family {
public:
Family(string name, int age) {
m_name = name;
m_age = age;
}
string m_name;
int m_age;
};
class MyCompareFamily {
public:
bool operator()(const Family&f1, const Family&f2) {
return f1.m_age > f2.m_age;
}
};
void test05() {
set<Family, MyCompareFamily>s2;
Family f1("渣渣猫", 26);
Family f2("土拨鼠", 25);
Family f3("锯嘴狐狸", 19);
Family f4("波波", 22);
s2.insert(f1);
s2.insert(f2);
s2.insert(f3);
s2.insert(f4);
for (set<Family, MyCompareFamily>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << "名字:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/dc765528ac264fbab99e581559560026.png)
void test01() {
pair<string, int>p(string("渣渣猫"), 100);
cout << "姓名:" << p.first << endl;
cout << "年龄:" << p.second << endl;
pair<string, int>p2 = make_pair("土拨鼠", 26);
cout << "姓名:" << p2.first << endl;
cout << "年龄:" << p2.second << endl;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/b7164f6ab0c04468a043f0d4e1e0e71d.png)