Fork me on GitHub

STL——容器(Set & multiset)的查找

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 }

打印结果:

 

 

 

 

 

 

 

 

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

posted @ 2020-06-10 02:03  索智源  阅读(322)  评论(0编辑  收藏  举报