一、 set基本概念
简介:
所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
二、 set构造和赋值
功能描述:创建set容器以及赋值
构造:
set<T> st;
//默认构造函数:
set(const set &st);
//拷贝构造函数
赋值:
set& operator=lconst set &st);
//重载等号操作符
set容器特点:
所有元素插入的时候自动被排序
set容器不允许插入重复值
#include<iostream>
using namespace std;
#include<set>
void print(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
set<int> s1;
//插入只有insert
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(40);
s1.insert(30);
print(s1);//10 30 40 50
set<int> s2(s1);
print(s2);//10 30 40 50
set<int> s3;
s3 = s1;
print(s3);//10 30 40 50
}
int main()
{
test1();
return 0;
}
三、 set大小和交换
函数原型:
size();
//返回容器中元素的数目
empty();
//判断容器是否为空
swap(st);
//交换两个集合容器
#include<iostream>
using namespace std;
#include<set>
//void print(set<int>& s)
//{
// for (set<int>::iterator it = s.begin(); it != s.end(); it++)
// {
// cout << *it << " ";
// }
// cout << endl;
//}
void test1()
{
set<int> s1;
//插入只有insert
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(40);
s1.insert(30);
if (s1.empty())
{
cout << "s1为空 " << endl;
}
else
{
cout << "s1不为空 " << endl;
cout << "s1的大小:" << s1.size() << endl;//4
}
}
int main()
{
test1();
return 0;
}
四、set插入和删除
函数原型:
insert(elem);
//在容器中插入元素。
clear();
//清除所有元素
erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);
//删除容器中值为elem的元素
#include<iostream>
using namespace std;
#include<set>
void print(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
set<int> s1;
//插入只有insert
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(40);
s1.insert(30);
print(s1);
//删除
s1.erase(s1.begin());
print(s1);//30 40 50
//删除
s1.erase(30);
print(s1);//40 50
//清空
s1.erase(s1.begin(), s1.end());
print(s1);
//清空
s1.clear();
print(s1);
}
int main()
{
test1();
return 0;
}
五、查找和统计
函数原型:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);
//统计key的元素个数
#include<iostream>
using namespace std;
#include<set>
//void print(set<int>& s)
//{
// for (set<int>::iterator it = s.begin(); it != s.end(); it++)
// {
// cout << *it << " ";
// }
// cout << endl;
//}
void test1()
{
set<int> s1;
//插入只有insert
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(40);
s1.insert(30);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到元素:" << *pos << endl;
}
else
cout << "未找到元素" << endl;
int num = s1.count(30);
cout << "num=" << num << endl;
}
int main()
{
test1();
return 0;
}
六、 set和multiset区别
区别:
set不可以插入重复数据,而multiset可以
set插入数据的同时会返回插入结果,表示插入是否成功
multiset不会检测数据,因此可以插入重复数据
七、 pair对组创建
功能描述:
成对出现的数据,利用对组可以返回两个数据
两种创建方式:
pair<type,type>p( value1,value2 );
pair<type,type>p=make_pair( value1,value2 );
#include<iostream>
using namespace std;
void test()
{
//第一种方式
pair<string, int>p1("Tom", 18);
cout << "姓名:" << p1.first << "年龄:" << p1.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("Jerry", 20);
cout << "姓名:" << p2.first << "年龄:" << p2.second << endl;
}
int main()
{
test();
return 0;
}
八、set容器排序
利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<set>
class compare
{
public:
bool operator()(int a,int b) const
{
return a > b;
}
};
void test()
{
set<int> s1;
s1.insert(20);
s1.insert(30);
s1.insert(10);
s1.insert(50);
s1.insert(40);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int,compare> s2;
s2.insert(20);
s2.insert(30);
s2.insert(10);
s2.insert(50);
s2.insert(40);
for (set<int>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test();
return 0;
}
九、 set容器排序(自定义数据类型)
#include<iostream>
using namespace std;
#include<set>
class person
{
public:
person(string name, int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
class compare
{
public:
bool operator()(const person &p1,const person&p2) const
{
return p1.age < p2.age;
}
};
void test()
{
set<person, compare> s1;
person p1("张三", 26);
person p2("李四", 50);
person p3("王五", 46);
person p4("赵六", 45);
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).name << "年龄:" << (*it).age << " ";
}
cout << endl;
}
int main()
{
test();
return 0;
}