目录
set/multiset容器
set的基本概念
1.所有元素都会在插入时自动排序
2.set/multiset属于关联式容器,底层结构是用二叉树来实现的
3.set和multiset区别:(1)set不允许容器中有重复的元素
(2)multiset允许容器中有重复的元素
set构造和赋值
#include<iostream>
using namespace std;
#include<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;
//插入数据的时候只有insert
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
s1.insert(30);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值,插入了重复值也不会报错,但是打印的时候只会打印出
//一个这样的值,不会打印重复的
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
}
int main()
{
system("pause");
return 0;
}
set大小和交换
#include<iostream>
using namespace std;
#include<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;
//插入数据的时候只有insert
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
s1.insert(30);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值,插入了重复值也不会报错,但是打印的时候只会打印出
//一个这样的值,不会打印重复的
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);
s2.insert(500);
cout << "交换前: " << endl;
printSet(s1);
printSet(s2);
s1.swap(s2);
cout << "交换后: " << endl;
printSet(s1);
printSet(s2);
}
int main()
{
test01();
system("pause");
return 0;
}
set插入和删除
#include<iostream>
using namespace std;
#include<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;
//插入数据的时候只有insert
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
s1.insert(30);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值,插入了重复值也不会报错,但是打印的时候只会打印出
//一个这样的值,不会打印重复的
printSet(s1);
//删除
s1.erase(s1.begin());
//s1.erase(s1.begin(), s1.end());
printSet(s1);
//删除重载版本
s1.erase(30);
printSet(s1);
//清空
s1.clear();
}
int main()
{
test01();
system("pause");
return 0;
}
set查找和统计
find(key) //查找key是否存在,如果存在返回该键的元素迭代器,不存在,返回set.end();
count(key) //统计key的元素个数
#include<iostream>
using namespace std;
#include<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;
//插入数据的时候只有insert
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
s1.insert(30);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值,插入了重复值也不会报错,但是打印的时候只会打印出
//一个这样的值,不会打印重复的
printSet(s1);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到元素: " << *pos << endl;
}
else {
cout << "未找到元素: " << *pos << endl;
}
//统计30 的个数
int num = s1.count(30);
//对于set而言,统计的结果要么为0,要么为1
cout << "num= " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
set和multiset的区别
如果不需要插入重复数据利用set
如果需要插入重复数据利用multiset
#include<iostream>
using namespace std;
#include<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>s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else {
cout << "第一次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else {
cout << "第一次插入失败" << endl;
}
multiset<int>ms;
//允许插入重复值
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
set容器排序
set容器默认排序规则是从小到大
利用仿函数,我们可以改变排序规则,要注意的是用类实现的仿函数要讲类放到打印函数的上面
#include<iostream>
using namespace std;
#include<set>
#include<map>
class Myclass {
public:
bool operator()(const int& v1, const int& v2)const
{
return v1 > v2;
}
};
void printSet(set<int>s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
cout << endl;
}
void test01()
{
set<int>s1;//自动排序,默认是从小到大排序
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(50);
printSet(s1);
//指定排序规则为从大到小
set< int,greater<int> >s2;
s2.insert(10);
s2.insert(20);
//不能用函数来打印//要是用class类来设置仿函数记着要将类放到打印函数的上面
for (set<int>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
pair对组创建
两种方式
pair<type,type> p (value1,value2);
pair<type,type> p = make_pair(vlaue1,value2);
#include<iostream>
using namespace std;
#include<set>
void test01()
{
//第一种方式
pair<string, int>p("tom", 20);
cout << "姓名: " << p.first << " 年龄: " << p.second<<endl;
//第二种方式
pair<string, int> p2 = make_pair("frank", 18);
cout << "姓名: " << p2.first << " 年龄: " << p2.second<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
这样的话函数的返回值可以是多个值,我们可以用pair来返回
map/multimap容器
map容器的基本概念
简单介绍:
1.map中所有元素都是pair
2.pair中的第一个元素是key(键值),起到索引作用,第二个元素是value(实值),我们俗称键值对
3.所有元素都会根据元素的键值自动排序
本质:关联式的容器
优点:
1.可以根据key值快速找到vlaue值
map和multimap区别:
1.map不允许容器中有重复key值元素,可以有重复的value值
2.multimap允许有重复key值元素,可以有重复的value值
map构造和赋值
#include<iostream>
using namespace std;
#include<set>
#include<map>
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
//拷贝构造
map<int, int>m2(m);
printMap(m);
//赋值
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main()
{
test01();
system("pause");
return 0;
}
map大小和交换
#include<iostream>
using namespace std;
#include<set>
#include<map>
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
cout << "交换前: " << endl;
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
if (m.empty())
{
cout << "m为空" << endl;
}
else {
cout << "m不为空" << endl;
cout << "m的大小为: " << m.size() << endl;
}
map<int, int>m1;
m1.insert(pair<int, int>(4, 110));
m1.insert(pair<int, int>(5, 70));
m1.insert(pair<int, int>(6, 80));
printMap(m1);
m.swap(m1);
cout << "交换后: " << endl;
printMap(m);
printMap(m1);
}
int main()
{
test01();
system("pause");
return 0;
}
map插入和删除
#include<iostream>
using namespace std;
#include<set>
#include<map>
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
map<int, int>m;
//第一种插入方法
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
//第二种插入方法
m.insert(make_pair(2, 20));
//第三种
m.insert(map<int, int>::value_type(3, 30));
//第四种//不建议
m[4] = 40;
//不建议[]来插入,用途:可以利用key访问到value
cout << m[5] << endl;//如果没有key为5的,会自动创建一个key为5,value为0的键值对
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);//只会按照key的值来删除
printMap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
map查找和统计
find(key); //查找key是否存在,若存在,返回该键的元素迭代器,返回map.end()
count(key) //统计key的元素个数
#include<iostream>
using namespace std;
#include<set>
#include<map>
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>::iterator pos = m.find(3);
if (pos != m.end()) {
cout << "查到了元素key = " << (*pos).first << " value= " << (*pos).second << endl;
}
else {
cout << "没有找到" << endl;
}
//统计操作
//map不允许插入重复key元素,count统计而言,结果要么为0,要么为1
//multimap允许插入重复key元素,count统计而言,结果可以大于1
int num = m.count(3);
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
map排序(默认按照键的从小到大排序,但是我们可以更改)
我们可以在map<int,int>m1;<>里面加一个参数,map<int,int,greater<int>>m1;来改变排序
#include<iostream>
using namespace std;
#include<set>
#include<map>
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
map<int, int>m;//会自动按照键的大小来排序
m.insert(pair<int, int>(4, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
//用greater<int>来逆序排序
map<int, int, greater<int>>m2;
m2.insert(pair<int, int>(4, 10));
m2.insert(pair<int, int>(2, 20));
m2.insert(pair<int, int>(3, 30));
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++)
{
cout << "key = " << (*it).first << " value = " << (*it).second << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
STL(上)和STL(下)参考19 STL初识-STL的基本概念_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1et411b73Z?p=185&vd_source=be84a5352adb0d33aedc2d3e1919d8aa