set容器构造和赋值
所有元素都会在插入时自动被排序
本质:set/multiset 属于关联式容器,底层结构是用二叉树实现的。
区别:set不允许容器中有重复元素,multiset允许容器有重复元素
构造:
set st; //默认构造函数
set(const set &st); //拷贝构造函数
赋值:
set& operator = (const set &st); //重载等号操作符
set容器插入数据只有insert的方式
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>&s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
printSet(s1);
//便历容器
//set容器的特点,所有元素在插入的时候都会被自动排序
//set容器不允许插入重复的值
//拷贝构造
set<int>s2(s1);
printSet(s2);
set<int>s3;
//赋值方式
s3 = s2;
printSet(s3);
}
int main() {
test01();
}
set容器大小和交换
统计set容器的大小以及交换set容器
size( ); //返回容器中元素的数目,set容器不支持resize( )的操作;
empty( ); //判断容器是否为空
swap( ); //交换两个集合容器
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>&s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
printSet(s1);
if (s1.empty())
{
cout << "集合s1为空" << endl;
}
else
{
cout << "集合s1不为空" << endl;
cout << "集合s1的大小是" << s1.size() << endl;
}
set<int>s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
cout << "交换前s1 s2分别是" << endl;
printSet(s1);
printSet(s2);
cout << "交换后s1 s2分别是" << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main() {
test01();
}
set容器插入和删除
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>&s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(100);
s1.insert(200);
s1.insert(300);
s1.insert(400);
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1);
s1.erase(300);
printSet(s1);
s1.clear();
cout << "s1清空以后是" << endl;
printSet(s1);
}
int main() {
test01();
}
set容器查找和统计
对set容器进行查找数据以及统计数据
find(key); //查找key是否存在,如果存在,返回该键的元素的迭代器;如果不存在,返回set.end( );
count(key); //统计key的元素个数;
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>&s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(100);
s1.insert(200);
s1.insert(300);
s1.insert(400);
printSet(s1);
//查找
set<int>::iterator pos = s1.find(200);
if (pos != s1.end())
{
cout << "找到了元素" << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
int num = s1.count(30);
cout << "set1中的30元素的个数是" << num << endl;
int num1 = s1.count(300);
cout << "set1中的300元素的个数是" << num1 << endl;
}
int main() {
test01();
}
set和multiset的区别
set不可以插入重复数据,multiset可以
set插入数据的同时会返回插入结果,表示插入是否成功
multiset不会检测数据,因此可以插入重复数据
#include<iostream>
using namespace std;
#include<set>
void test01() {
set<int>s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if(ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s.insert(100);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
multiset<int>ms;
ms.insert(109);
ms.insert(110);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
}
pair对组的创建
成对出现的数据,利用对组可以返回两个数据
两种创建方式:
pair<type,type> p (value1,value2);
pair<type,type> p = make_pair(value1,value2)
#include<iostream>
using namespace std;
#include<set>
#include<string>
void test01() {
//第一种方式
pair<string, int>p("Tom", 20);
cout << "姓名是" << p.first <<" " << "年龄是" << p.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("Jerry", 18);
cout << "姓名是" << p2.first <<" " << "年龄是" << p2.second << endl;
}
int main() {
test01();
}
内置类型指定排序规则
set容器默认排序是从小到大,掌握如何改变排序规则
主要技术点:利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<set>
#include<string>
class MyCompare
{
public:
bool operator()(int v1, int v2)const volatile
{
return v1 > v2;
}
};
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(90);
s1.insert(50);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int, MyCompare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(90);
s2.insert(50);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
}
自定义数据类型指定排序规则
#include<iostream>
using namespace std;
#include<set>
#include<string>
class Person
{
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class comparePerson {
public:
bool operator()(const Person &p1, const Person &p2)const volatile
{
//按照年龄降序排列
return p1.m_Age > p2.m_Age;
}
};
void test01() {
set<Person,comparePerson>s1;
Person p1("刘备", 24);
Person p2("关羽", 28);
Person p3("张飞", 25);
Person p4("赵云", 21);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
for (set<Person>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << "姓名是" << (*it).m_Name << " " << "年龄是" << (*it).m_Age<< endl;
}
}
int main() {
test01();
}