1. set.find(elem);

//查找elem元素,返回指向elem元素的迭代器。

 1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> setInt;
9
10 cout << "插入20个元素" << endl << endl;
11 for (int i = 0; i < 20; i++)
12 {
13 setInt.insert(i);
14 }
15
16 //使用 set.find(elem) 查找元素 10,返回指向元素 10 的迭代器
17 cout << "输入一个想要查找的int元素:";
18 int Num_1 = 0;
19 cin >> Num_1;
20
21 set<int>::iterator it_1 = setInt.find(Num_1);
22 if (it_1 != setInt.end()) //这里注意下,find 查找是一个逐个遍历的过程,他最终会拿到 end() 方法
23 {
24 cout << "拿了元素:" << *it_1 << endl;
25 }
26 else
27 {
28 cout << "没有拿到期望的元素 " << Num_1 << endl;
29 }
30
31 cout << "输入一个想要查找的int元素:";
32 int Num_2 = 0;
33 cin >> Num_2;
34
35 set<int>::iterator it_2 = setInt.find(Num_2);
36 if (it_2 != setInt.end()) //这里注意下,find 查找是一个逐个遍历的过程,他最终会拿到 end() 方法
37 {
38 cout << "拿了元素:" << *it_2 << endl;
39 }
40 else
41 {
42 cout << "没有拿到期望的元素 " << Num_2 << endl;
43 }
44
45 return 0;
46 }

打印结果:

2. set.count(elem);

//返回容器中值为elem的元素个数。对set来说,要么是0,要么是1。对multiset来说,值可能大于1。

 1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> setInt_1;
9 cout << "set 中插入了2个1,3个2,1个3" << endl;
10 setInt_1.insert(1);
11 setInt_1.insert(1);
12 setInt_1.insert(2);
13 setInt_1.insert(2);
14 setInt_1.insert(2);
15 setInt_1.insert(3);
16 cout << "输入想要查询的元素:";
17 int Num_1 = 0;
18 cin >> Num_1;
19 cout << "找到了" << setInt_1.count(Num_1) << "个元素" << Num_1 << endl;
20
21 cout << endl;
22 multiset<int> setInt_2;
23 cout << "multiset 中插入了2个1,3个2,1个3" << endl;
24 setInt_2.insert(1);
25 setInt_2.insert(1);
26 setInt_2.insert(2);
27 setInt_2.insert(2);
28 setInt_2.insert(2);
29 setInt_2.insert(3);
30 cout << "输入想要查询的元素:";
31 int Num_2 = 0;
32 cin >> Num_2;
33
34 cout << "找到了" << setInt_2.count(Num_2) << "个元素" << Num_2 << endl;
35
36 return 0;
37 }

打印结果:

3. set.lower_bound(elem); 以及 set.upper_bound(elem);

set.lower_bound(elem);//返回第一个>=elem元素的迭代器。

set.upper_bound(elem);//返回第一个>elem元素的迭代器。

 1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> setInt;
9 for (int i = 0; i < 10; i++)
10 {
11 setInt.insert(i);
12 }
13
14 cout << "输入一个 int 元素:";
15 int Num = 0;
16 cin >> Num;
17
18 set<int>::iterator it_1 = setInt.lower_bound(Num);
19 set<int>::iterator it_2 = setInt.upper_bound(Num);
20 cout << "lower_bound 返回迭代器的元素为:" << *it_1 << endl;
21 cout << "lower_bound 返回迭代器前一个元素为:" << *--it_1 << endl;
22 cout << "upper_bound 返回迭代器的元素为:" << *it_2 << endl;
23
24 return 0;
25 }

打印结果:

5. set.equal_range(elem);

//返回容器中与elem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。函数返回两个迭代器,而这两个迭代器被封装在pair中。

 1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> setInt;
9 cout << "插入1个1,3个2,2个3";
10 setInt.insert(1);
11 setInt.insert(2);
12 setInt.insert(2);
13 setInt.insert(2);
14 setInt.insert(3);
15 setInt.insert(3);
16 cout << endl;
17
18 cout << "1-3 输入一个数字:";
19 int Num = 0;
20 cin >> Num;
21
22 // equal_range 的第一个迭代器会返回第一个等于参数的元素
23 // equal_range 第二个迭代器会返回第一个不等于参数的元素
24 pair<set<int>::iterator, set<int>::iterator> it = setInt.equal_range(2);
25
26 cout << "第一个迭代器的值为:" << *(it.first) <<endl;
27 cout << "第二个迭代器的值为:" << *(it.second) << endl;
28
29 return 0;
30 }

打印结果:

===================================================================================================================================

STL——容器(Set & multiset)的查找的更多相关文章

  1. C++STL容器(lower_bound,upper_bound)

    C++STL容器中有三种二分查找函数,这里分享其中的两个 这两个函数其实都可以理解为不破坏数组次序的前期下能将目标元素插入到数组的第几个位置,不过在细节上两个函数有所差异 int d[6]={0,2, ...

  2. STL Set和multiset 容器

    STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...

  3. STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值

    1. 默认构造 set<int> setInt;              //一个存放int的set容器. set<float> setFloat;          //一 ...

  4. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  5. STL容器的适用情况

     转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...

  6. STL容器的本质

    http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...

  7. STL - set和multiset

    set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...

  8. c++ STL容器初探

    什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...

  9. C++ STL set和multiset的使用

    C++ STL set和multiset的使用 std::set<int> s;那个s这个对象里面存贮的元素是从小到大排序的,(因为用std::less作为比较工具.) 1,set的含义是 ...

  10. STL 容器简介

    一.概述 STL 对定义的通用容器分三类:顺序性容器.关联式容器和容器适配器. 顺序性容器是一种各元素之间有顺序关系的线性表.元素在顺序容器中保存元素置入容器时的逻辑顺序,除非用删除或插入的操作改变这 ...

随机推荐

  1. Mon失效处理方法

    假设环境为三个mon,主机名为mon1.mon2.mon3,现在mon3上面的系统盘损坏,mon的数据完全丢失,现在需要对mon3进行恢复处理 1.停止所有mon进程,可以不停其他进程,需要停止内核客 ...

  2. 利用HUtool读取Excel内容

    // 1.获取上传文件输入流 InputStream inputStream = null; try{ inputStream = file.getInputStream(); }catch (Exc ...

  3. Codeforces1009F Dominant Indices

    dsu on tree 题目链接 点我跳转 题目大意 给定一棵以 \(1\) 为根,\(n\) 个节点的树.设\(d(u,x)\) 为 \(u\) 子树中到 \(u\) 距离为 \(x\) 的节点数. ...

  4. 在Guitar Pro中如何调节拍

    编辑一首曲子的时候,曲子的节奏往往是很重要的,那节奏一般是怎样设置的呢,在{cms_selflink page='index' text='Guitar Pro'}中,一般是通过调节节拍来完成的,很多 ...

  5. MathType总结编辑括号的类型(上)

    括号的种类有很多,我们用得也很多,可以说无处不见,不只是在数学物理这些自然科学的公式中来断地出现括号,即使是在人文艺术类的领域也会有括号的出现.下面就和小编一起来看看公式编辑器编辑括号的类型吧! Ma ...

  6. 接上一篇:(三) Spring环境搭建

    3.1.获取 Spring framework jar 包 (一) spring官网下载 (二)spring的核心包 (三) 配置 XML 1. 新建立一个 xml.名字任意,如 applicatio ...

  7. Pytest自动化测试 - 必知必会的一些插件

    Pytest拥有丰富的插件架构,超过800个以上的外部插件和活跃的社区,在PyPI项目中以" pytest- *"为标识. 本篇将列举github标星超过两百的一些插件进行实战演示 ...

  8. C语言讲义——变量(variable)

    变量(variable) 变量用于存放数据 变量是供程序操作的存储区的名字 变量有类型,该类型决定了变量占用内存的大小 字节→ C语言有以下6种简单变量类型: 类型细分: 变量在内存中需要占据空间,内 ...

  9. 小米死磕硬核技术,将扩招5000名工程师,多个领域会使用到C++

    小米MIDC大会2020在北京小米科技园召开,小米集团创始人.集团董事长兼CEO雷军宣布:"重视人才队伍的建设.人才是创新之源,提升技术实力的第一步,就是聚拢最顶尖的人才.小米2021年将在 ...

  10. 【JVM】jdk1.8移除方法区与metaspace

    转载:https://blog.csdn.net/aa747604141/article/details/52673582 https://www.jianshu.com/p/a6f19189ec62