#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//set mutiset 头文件
#include <set>
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值
set容器常用函数
使用时注意包含头文件<set> std::set and std::multiset associative containers
/*
s.begin() 返回set容器的第一个元素
s.end() 返回set容器的最后一个元素
s.clear() 删除set容器中的所有的元素
s.empty() 判断set容器是否为空
s.insert() 插入一个元素
s.erase() 删除一个元素
s.size() 返回当前set容器中的元素个数
*/
void PrintSet(set<int>&s) //按顺序输出容器内的元素
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//关联式容器,根据K值进行排序 从小到大排序
s1.insert(5);
s1.insert(1);
s1.insert(7);
s1.insert(2);
s1.insert(9);
PrintSet(s1);
s1.erase(s1.begin());
s1.erase(2);
PrintSet(s1);
}
void test02()
{
set<int>s1;
s1.insert(5);
s1.insert(1);
s1.insert(7);
s1.insert(2);
s1.insert(9);
//1.find(key) 查找key是否存在,存在则返回该键元素的迭代器 没有则返回set.end()
//对于set容器 没有 value key就是value
set<int>::iterator pos = s1.find(2);
//判断是否找到
if (pos != s1.end())
{
cout << "找到了,值为:" << *pos << endl;
}
else
{
cout << "未找到" << endl;
}
//2.count(key) 查找key的个数
cout<<"元素为1的个数有"<<s1.count(1)<<"个"<<endl;
//3.lower_bound(keyElement) 返回第一个key >= keyElement元素的迭代器
set<int>::iterator pos2 =s1.lower_bound(3);
if (pos2 != s1.end())
{
cout << "找到了,值为:" << *pos2 << endl;
}
else
{
cout << "未找到" << endl;
}
//4.upper_bound(keyElement) 返回第一个key > keyElement元素的迭代器
set<int>::iterator pos3 = s1.lower_bound(3);
if (pos3 != s1.end())
{
cout << "找到了,值为:" << *pos3 << endl;
}
else
{
cout << "未找到" << endl;
}
//5.equal_range(keyElement) 返回key = keyElement相等的上下限的两个迭代器
//上下限 就是 lower_bound 和 upper_bound
pair<set<int>::iterator, set<int>::iterator> ret = s1.equal_range(2);
//获取第一个值 ret.first
if (ret.first != s1.end())
{
cout << "找到了equal_range的第一个值lower_bound为:" << *(ret.first) << endl;
}
else
{
cout << "未找到" << endl;
}
//获取第二个值 ret.second
if (ret.second != s1.end())
{
cout << "找到了equal_range的第二个值upper_bound为:" << *(ret.second) << endl;
}
else
{
cout << "未找到" << endl;
}
}
//set容器,不允许插入重复的
void test03()
{
set<int>s1;
s1.insert(10);
s1.insert(10);
PrintSet(s1);//插入两个值,只能打印出一个 说明第二次插入已经失败了
//判断再次插入是否插入成功
pair<set<int>::iterator, bool> p = s1.insert(10);
if (p.second)
{
cout<<"插入成功"<<endl;
}
else
{
cout << "插入失败" << endl;
}
}
//指定排序规则
//仿函数
class myCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
//set容器排序
void test04()
{
set<int,myCompare>s1;
s1.insert(5);
s1.insert(1);
s1.insert(7);
s1.insert(2);
s1.insert(9);
//PrintSet(s1);//内部已排序好了
//从大到小排序 必须在插入前就指定排序规则
for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << endl;
}
}
//set 容器插入自定义数据类型
class Person
{
public:
Person(string Name, int age)
{
this->m_Name = Name;
this->m_age = age;
}
string m_Name;
int m_age;
};
class MyCompare
{
public:
bool operator()(const Person &p1, const Person &p2)
{
if (p1.m_age > p2.m_age)
{
return true;
}
else
return false;
}
};
void test05()
{
Person p1("大娃",100);
Person p2("二娃", 80);
Person p3("三娃", 10);
Person p4("四娃", 60);
Person p5("五娃", 200);
set<Person,MyCompare> s1;
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
s1.insert(p5);
//会编译失败。set不知道以什么方式排序 利用仿函数 指定排序规则 方法与上相同
for (set<Person, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_age << " " << endl;
}
}
int main()
{
//test01();
//test02();
//test03();
//test04();
test05();
std::cout << "Hello World!\n";
}
set容器的基本操作
最新推荐文章于 2024-05-18 17:16:57 发布