第九层(8):STL之set/multiset(下)

简介: 第九层(8):STL之set/multiset(下)

查找函数


在set中还可以查找一个元素是否存在,查找方式是通过key

find(key);//查找key是否存在,存在返回元素的迭代器,不存在返回end()


使用:


#include<iostream>
#include<set>
using namespace std;
void test1()
{
  set<int> s;
  for (int i = 0; i < 10; i++)
  {
  s.insert(i);
  }
  set<int>::iterator f=s.find(10);
  if (f != s.end())
  {
  cout << "找到了" << endl;
  }
  else
  {
  cout << "没有找到" << endl;
  }
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


统计函数


可以统计这个容器有多少个这个元素,对于set来说,只有0和1,因为set是不可以拥有重复数据的

count(key);//统计key的个数


使用:


#include<iostream>
#include<set>
using namespace std;
void test1()
{
  set<int> s;
  for (int i = 0; i < 10; i++)
  {
  s.insert(i);
  }
  cout << s.count(10) << endl;
  cout << s.count(7) << endl;
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


为什么set不可以插入重复数据


那为什么multiset可以插入重复数据,set不可以呢?它们的区别就在这里,其他都一样,这是因为什么?是因为在set插入数据的时候,会返回插入结果,表示是否插入成功,但是multiset不会,那set怎么去检测插入是否成功呢?这里可以使用pair数组,因为对于set插入失败成功所返回的值就是用pair数组去接收的


pair数组


pair数组是一个拥有两种数据类型的数组,都需要使用的时候去规定,在set中,已经封装好pair数组的两个元素了,第一个为迭代器,第二个是bool,这个时候,如果插入成功,bool会返回真,如果没有插入成功,会返回假,可以去使用一下,在set插入中pair的接收是这样的:

pair< set< T >:: iterator , bool >;


检测:


#include<iostream>
#include<set>
using namespace std;
void test1()
{
  set<int> s;
  pair<set<int>::iterator, bool> in = s.insert(10);
  pair<set<int>::iterator, bool> in1 = s.insert(10);
  if (in.second/*访问第二个数据类型的元素*/)
  {
  cout << "插入成功" << endl;
  }
  else 
  {
  cout << "插入失败" << endl;
  }
  if (in1.second/*访问第二个数据类型的元素*/)
  {
  cout << "插入成功" << endl;
  }
  else
  {
  cout << "插入失败" << endl;
  }
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


pair数组创建


在有两个成对的数据出现的时候,就可以利用pair接收两个数据,那怎么去创建一个pair数组呢?有两种方式

pair< T1, T2/*数据类型*/ > p(t1 ,t2/*数据*/);
pair< T1, T2/*数据类型*/ > p=make_pair(t1 ,t2/*数据*/);


使用:


#include<iostream>
#include<set>
using namespace std;
void test1()
{
  pair<int, int > p(10, 20);
  cout << p.first << " " << p.second << endl;
  pair<int, int>p1 = make_pair(10, 20);
  cout << p1.first << " " << p1.second << endl;
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


怎么样去改变set容器的排序规则


如果现在创建了一个set容器,但是不想让它按着从小到大的顺序排,想按照从大到小排,可以吗?是可以的,可以利用仿函数去改变,将有这个仿函数的类当类型传给set容器

使用:


#include<iostream>
#include<set>
using namespace std;
class sort_big
{
public:
  bool operator()(int v1, int v2) const
  {
  return v1 > v2;
  }
};
void test1()
{
  set<int, sort_big> s;
  for (int i = 0; i < 10; i++)
  {
  s.insert(i);
  }
; for (set<int,sort_big>::iterator b = s.begin(); b != s.end(); b++)
  {
  cout << *b << " ";
  }
}
int main()
{
  test1();
  return 0;
}

2d65d23f6d4748949b924e4057485923.png


下一座石碑


这座石碑倒下了,露出了下一座石碑…


😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔

🚀专栏:C++爬塔日记

🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉


相关文章
|
5月前
|
C++ 容器
【C++】红黑树模拟实现STL中的map与set
【C++】红黑树模拟实现STL中的map与set
|
4月前
|
C++ 容器
C++之set/multiset容器
C++之set/multiset容器
|
4月前
|
存储 自然语言处理 C++
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
36 0
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
|
4月前
|
存储 编译器 C++
|
3月前
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
52 0
|
5月前
|
存储 C语言 容器
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别(下)
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别
40 1
|
5月前
|
存储 C语言 容器
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别(中)
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别
43 1
|
5月前
|
存储 自然语言处理 C语言
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别(上)
从C语言到C++_26(set+map+multiset+multimap)力扣692+349+牛客_单词识别
52 1
|
5月前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
38 4
|
5月前
|
存储 Serverless C++
【C++入门到精通】哈希 (STL) _ unordered_map _ unordered_set [ C++入门 ]
【C++入门到精通】哈希 (STL) _ unordered_map _ unordered_set [ C++入门 ]
53 1