C++STL中list, vector, map, set用法与区别_c++ stl list map 比较

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

1、map创建、元素插入和遍历访问
     创建map对象,键值与映照数据的类型由自己定义。在没有指定比较函数时,元素的插入位置是按键值由小到大插入到黑白树中去的,下面这个程序详细说明了如何操作map容器。

 #include <map>
 #include <string>
 #include <iostream>

 using std :: cout ;
 using std :: endl ;
 using std :: string ;
 using std :: map ;
 
int main()
{
     //定义map对象,当前没有任何元素
     map<string,float> m ;
     
     //插入元素,按键值的由小到大放入黑白树中
     m["Jack"] = 98.5 ;
     m["Bomi"] = 96.0 ;
     m["Kate"] = 97.5 ;
     
    //先前遍历元素
     map<string,float> :: iterator it ;
     for(it = m.begin() ; it != m.end() ; it ++)
          cout << (\*it).first << " : " << (\*it).second << endl ;

     return 0 ;
}

// 运行结果:
    Bomi :96
    Jack  :98.5
    Kate  :97.5

程序编译试,会产生代号为“warning C4786” 的警告, “4786” 是标记符超长警告的代号。可以在程序的头文件包含代码的前面使用"#pragma waring(disable:4786)" 宏语句,强制编译器忽略该警告。4786号警告对程序的正确性和运行并无影响。
2、删除元素
     map映照容器的 erase() 删除元素函数,可以删除某个迭代器位置上的元素、等于某个键值的元素、一个迭代器区间上的所有元素,当然,也可使用clear()方法清空map映照容器。
下面这个程序演示了删除map容器中键值为28的元素:

 #include <map>
 #include <string>
 #include <iostream>

 using std :: cout ;
 using std :: endl ;
 using std :: string ;
 using std :: map ;
 
int main()
{
    //定义map对象,当前没有任何元素
    map<int, char> m ;
    //插入元素,按键值的由小到大放入黑白树中
    m[25] = 'm' ;
    m[28] = 'k' ;
    m[10] = 'x' ;
    m[30] = 'a' ;
    //删除键值为28的元素
    m.erase(28) ;
    //向前遍历元素
    map<int, char> :: iterator it ;
   for(it = m.begin() ; it != m.end() ; it ++)
   {
        //输出键值与映照数据
        cout << (\*it).first << " : " << (\*it).second << endl ;
    }
    return 0 ;
}



运行结果:

10 : x
25 : m
30 : a

3、元素反向遍历
     可以用反向迭代器reverse_iterator反向遍历map映照容器中的数据,它需要rbegin()方法和rend()方法指出反向遍历的起始位置和终止位置。

 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定义map对象,当前没有任何元素
13    map<int, char> m ;
14    //插入元素,按键值的由小到大放入黑白树中
15    m[25] = 'm' ;
16    m[28] = 'k' ;
17    m[10] = 'x' ;
18    m[30] = 'a' ;
19    //反向遍历元素
20    map<int, char> :: reverse_iterator rit ;
21    for( rit = m.rbegin() ; rit != m.rend() ; rit ++)
22    {
23        //输入键值与映照数据
24        cout << (\*rit).first << " : " << (\*rit).second << endl ;
25    }
26    return 0 ;
27}
28

运行结果:

                  30 : a
                  28 : k
                  25 : m
                  10 : x

4、元素的搜索
    使用find()方法来搜索某个键值,如果搜索到了,则返回该键值所在的迭代器位置,否则,返回end()迭代器位置。由于map采用黑白树数据结构来实现,所以搜索速度是极快的。
下面这个程序搜索键值为28的元素:

 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定义map对象,当前没有任何元素
13    map<int, char> m ;
14    //插入元素,按键值的由小到大放入黑白树中
15    m[25] = 'm' ;
16    m[28] = 'k' ;
17    m[10] = 'x' ;
18    m[30] = 'a' ;
19    map<int, char> :: iterator it ;
20    it = m.find(28) ;
21    if(it != m.end())  //搜索到该键值
22            cout << (\*it).first << " : " << ( \*it ).second << endl ;
23    else
24            cout << "not found it" << endl ;
25    return 0 ;
26}

5、自定义比较函数
    将元素插入到map中去的时候,map会根据设定的比较函数将该元素放到该放的节点上去。在定义map的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由小到大的顺序插入元素。在很多情况下,需要自己编写比较函数。
编写方法有两种。
    (1)如果元素不是结构体,那么,可以编写比较函数。下面这个程序编写的比较规则是要求按键值由大到小的顺序将元素插入到map中

 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10//自定义比较函数 myComp
11struct myComp
12{
13    bool operator() (const int &a, const int &b)
14    {
15        if(a != b) return a > b ;
16        else  return a > b ;
17    }
18} ;
19
20int main()
21{
22    //定义map对象,当前没有任何元素
23    map<int, char> m ;
24    //插入元素,按键值的由小到大放入黑白树中
25    m[25] = 'm' ;
26    m[28] = 'k' ;
27    m[10] = 'x' ;
28    m[30] = 'a' ;
29    //使用前向迭代器中序遍历map
30    map<int, char,myComp> :: iterator it ;
31    for(it = m.begin() ; it != m.end() ; it ++)
32            cout << (\*it).first << " : " << (\*it).second << endl ;
33    return 0 ;
34}

运行结果:

                  30 :a
                  28 :k
                  25 :m
                  10 :x

(2)如果元素是结构体,那么,可以直接把比较函数写在结构体内。下面的程序详细说明了如何操作:

1#include <map>
2#include <string>
3#include <iostream>
4
5using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
9
10struct Info
11{
12    string name ;
13    float score ;
14    //重载 “<”操作符,自定义排列规则
15    bool operator < (const Info &a) const
16    {
17        //按score由大到小排列。如果要由小到大排列,使用“>”号即可
18        return a.score < score ;
19    }
20} ;
21
22int main()
23{
24    //定义map对象,当前没有任何元素
25    map<Info, int> m ;
26    //定义Info结构体变量
27    Info info ;
28    //插入元素,按键值的由小到大放入黑白树中
29    info.name = "Jack" ;
30    info.score = 60 ;
31    m[info] = 25 ;
32    info.name = "Bomi" ;
33    info.score = 80 ;
34    m[info] = 10 ;
35    info.name = "Peti" ;
36    info.score = 66.5 ;
37    m[info] = 30 ;
38    //使用前向迭代器中序遍历map
39    map<Info,int> :: iterator it ;
40    for(it = m.begin() ; it != m.end() ; it ++)
41    {
42            cout << (\*it).second << " : " ;
43            cout << ((\*it).first).name << " : " << ((\*it).first).score << endl ;
44    }
45    return 0 ;
46}

运行结果:

10 :Bomi   80
30 :Peti     66.5
25 :Jack    60

6、用map实现数字分离
    对数字的各位进行分离,采用取余等数学方法是很耗时的。而把数字当成字符串,使用map的映照功能,很方便地实现了数字分离。下面这个程序将一个字符串中的字符当成数字,并将各位的数值相加,最后输出各位的和。

 1#include <string>
 2#include <map>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定义map对象,当前没有任何元素
13    map<char, int> m ;
14
15    //赋值:字符映射数字
16    m['0'] = 0 ;
17    m['1'] = 1 ;
18    m['2'] = 2 ;
19    m['3'] = 3 ;
20    m['4'] = 4 ;
21    m['5'] = 5 ;
22    m['6'] = 6 ;
23    m['7'] = 7 ;
24    m['8'] = 8 ;
25    m['9'] = 9 ;
26    /\*\*//\*上面的10条赋值语句可采用下面这个循环简化代码编写
27 for(int j = 0 ; j < 10 ; j++)
28 {
29 m['0' + j] = j ;
30 }
31 \*/
32    string sa, sb ;
33    sa = "6234" ;
34    int i ;
35    int sum = 0 ;
36    for ( i = 0 ; i < sa.length() ; i++ )
37            sum += m[sa[i]] ;
38    cout << "sum = " << sum << endl ;
39    return 0 ;
40}

7、数字映照字符的map写法
     在很多情况下,需要实现将数字映射为相应的字符,看看下面的程序:

 1#include <string>
 2#include <map>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定义map对象,当前没有任何元素
13    map<int, char> m ;
14
15    //赋值:字符映射数字
16    m[0] = '0' ;
17    m[1] = '1' ;
18    m[2] = '2' ;
19    m[3] = '3' ;
20    m[4] = '4' ;
21    m[5] = '5' ;
22    m[6] = '6' ;
23    m[7] = '7' ;
24    m[8] = '8' ;
25    m[9] = '9' ;
26    /\*\*//\*上面的10条赋值语句可采用下面这个循环简化代码编写
27 for(int j = 0 ; j < 10 ; j++)
28 {
29 m[j] = '0' + j ;
30 }
31 \*/
32    int n = 7 ;
33    string s = "The number is " ;
34    cout << s + m[n] << endl ;
35    return 0 ;
36}

运行结果:

The number is 7

准模板库就是类与函数模板的大集合。STL共有6种组件:容器,容器适配器,迭代器,算法,函数对象和函数适配器。
1、容器:

容器是用来存储和组织其他对象的对象。STL容器类的模板在标准头文件中定义。主要如下所示

①序列容器

基本的序列容器是上面图中的前三类:

关于三者的优缺点主要是:

A:vector矢量容器:可以随机访问容器的内容,在序列末尾添加或删除对象,但是因为是从尾部删除,过程非常慢,因为必须移动插入或删除点后面的所有对象。

矢量容器的操作:(自己以前有个表,贴出来大家看看)

其中的capacity表示容量,size是当前数据个数。矢量容器如果用户添加一个元素时容量已满,那么就增加当前容量的一半的内存,比如现在是500了,用户添加进第501个,那么他会再开拓250个,总共就750个了。所以矢量容器当你添加数据量很大的时候,需要注意这一点哦。。。

如果想用迭代器访问元素是比较简单的,使用迭代器输出元素的循环类似如下:

vector::iterator表示矢量容器vector的迭代器。。。
for(vector::iterator iter = number.begin(); iter<number.end(); iter++)//这里的iterator iter算是一个指针了
cout << " " << *iter;
当然也可以用我们自己的方法,但是感觉用上面的更好一些。

for(vector::size_type i=0; i<number.size(); i++)
cout << " " << number[i]

排序矢量元素:

对矢量元素的排序可以使用头文件中定义的sort()函数模板来对一个矢量容器进行排序。但是有几点要求需要注意

sort()函数模板用<运算符来排列元素的顺序,所以容器中对象必须可以进行<运算,如果是基本类型,可以直接调用sort(),如果是自定义对象,必须对<进行运算符重载
两个迭代器的指向必须是序列的第一个对象和最后一个对象的下一个位置。比如:sort(people.begin(), people.end());//这里两个参数就是迭代器的意思了

B:deque容器:非常类似vector,且支持相同的操作,但是它还可以在序列开头添加和删除。

deque双端队列容器与矢量容器基本类似,具有相同的函数成员,但是有点不同的是它支持从两端插入和删除数据,所以就有了两个函数:push_front和pop_front。并且有两个迭代器变量

#include
deque data;//创建双端队列容器对象
deque::iterator iter;//书序迭代器
deque::reverse_iterator riter;//逆序迭代器。
//iter和riter是不同的类型

C:list容器是双向链表,因此可以有效的在任何位置添加和删除。列表的缺点是不能随机访问内容,要想访问内容必须在列表的内部从头开始便利内容,或者从尾部开始。

②关联容器

map<K, T>映射容器:K表示键,T表示对象,根据特定的键映射到对象,可以进行快速的检索。

有关它的创建以及查找的操作作如下总结

//创建映射容器
map<person, string> phonebook;

//创建要存储的对象
pair<person, string> entry = pair<person, string>(person(“mel”, “Gibson”), “213 345 567”);

//插入对象
phonebook.insert(entry);//只要映射中没有相同的键,就可以插入entry

//访问对象
string number = phonebook[person(“mel”, “Gibson”)];//如果这个键不存在,会默认将这个键插入

//如果不想在找不到的时候插入,可以先查找然后再检索
person key = person(“mel”, “Gibson”);
map<person, string>::iterator iter = phonebook.find(key);//创建迭代器,就认为是指针就好了

if(iter != phonebook.end())
string number = iter->second;

2、容器适配器:

容器适配器是包装了现有的STL容器类的模板类,提供了一个不同的、通常更有限制性的功能。具体如下所示

A:queue队列容器:通过适配器实现先进先出的存储机制。我们只能向队列的末尾添加或从开头删除元素。push_back() pop_front()

代码:queue<string, list > names;(这就是定义的一个适配器)是基于列表创建队列的。适配器模板的第二个类型形参指定要使用的底层序列容器,主要的操作如下

B:priority_queue优先级队列容器:是一个队列,它的顶部总是具有最大或最高优先级。优先级队列容器与队列容器一个不同点是优先级队列容器不能访问队列后端的元素。

默认情况下,优先级队列适配器类使用的是矢量容器vector,当然可以选择指定不同的序列容器作为基础,并选择一个备用函数对象来确定元素的优先级代码如下

priority_queue<int, deque, greate> numbers;

C:stack堆栈容器:其适配器模板在头文件中定义,默认情况下基于deque容器实现向下推栈,即后进先出机制。只能访问最近刚刚进去的对象

//定义容器
stack people;
//基于列表来定义堆栈
stack<string, list> names;

基本操作如下:

3、迭代器:

具体它的意思还没怎么看明白,书上介绍迭代器的行为与指针类似,这里做个标记奋斗,看看后面的例子再给出具体的解释

具体分为三个部分:输入流迭代器、插入迭代器和输出流迭代器。

看这一章的内容看的我有点抑郁了都,摘段课本介绍的内容,还是可以帮助理解的

头文件中定义了迭代器的几个模板:①流迭代器作为指向输入或输出流的指针,他们可以用来在流和任何使用迭代器或目的地之间传输数据。②插入迭代器可以将数据传输给一个基本序列容器。头文件中定义了两个流迭代器模板:istream_iterator用于输入流,ostream_iterator用于输出流。T表示从流中提取数据或写到流中的对象的类型。头文件还定义了三个插入模板:insert, back_insert和front_inset。其中T也是指代序列容器中数据的类型。

输入流迭代器用下面的程序来说明下,可见具体注释

#include
#include
#include
#include

using namespace std;

int main()
{
//定义矢量容器
vector numbers;
cout << “请输入整数值,以字母结束:”;

//定义输入流迭代器。注意两个不同  
//1、numberInput(cin)是指定迭代器指向流cin  
//2、numbersEnd没有指定,是默认的,默认构造了一个end_of_stream的迭代器,它等价于调用end()  
istream_iterator<int> numbersInput(cin), numbersEnd;  

//用户输入,直到输入的不是int类型或者终止时结束。   
while(numbersInput != numbersEnd)  
    numbers.push_back(*numbersInput++);  

cout << "打印输出:" << numbers.at(3) << endl;  


//如何指定输入流呢?  
  
//确定字符串  
string data("2.1 3.6 36.5 26 34 25 2.9 63.8");  

//指定data为输入流input。需要头文件<sstream>  
istringstream input(data);  

//定义迭代器  
istream_iterator<double> begin(input), end;  

//计算数值和。  
//acculumate为头文件<numeric>下定义的函数。  
//第一个参数是开始迭代器,第二个是终止迭代器(最后一个值的下一个)。第三个是和的初值,注意必须用0.0,用它确定数据类型是double  
cout << "打印数据的总和:" << accumulate(begin, end, 0.0) << endl;  

}
输出结果:

耽误时间太多。以后再写吧

4、算法:

算法是操作迭代器提供的一组对象的STL函数模板,对对象的一个操作,可以与前面的容器迭代器结合起来看。如下图介绍

5、函数对象:

函数对象是重载()运算符的类类型的对象。就是实现operator()()函数。

函数对象模板在头文件中定义,必要时我们也可以定义自己的函数对象。做个标记奋斗,等有具体实例来进行进一步的解释。

6、函数适配器:

函数适配器是允许合并函数对象以产生一个更复杂的函数对象的函数模板。

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:

Map<int, string> mapStudent;

map的构造函数



map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:


Map<int, string> mapStudent;


2. ```
  数据的插入

在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:

第一种:用insert函数插入pair数据,下面举例说明(以下代码虽然是随手写的,应该可以在VC和GCC下编译通过,大家可以运行下看什么效果,在VC下请加入这条语句,屏蔽4786警告 #pragma warning (disable:4786) )

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent.insert(pair<int, string>(1, “student_one”));

   mapStudent.insert(pair<int, string>(2, “student_two”));

   mapStudent.insert(pair<int, string>(3, “student_three”));

   map<int, string>::iterator  iter;

   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第二种:用insert函数插入value_type数据,下面举例说明

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent.insert(map<int, string>::value_type (1, “student_one”));

   mapStudent.insert(map<int, string>::value_type (2, “student_two”));

   mapStudent.insert(map<int, string>::value_type (3, “student_three”));

   map<int, string>::iterator  iter;

   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第三种:用数组方式插入数据,下面举例说明

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent[1] =  “student_one”;

   mapStudent[2] =  “student_two”;

   mapStudent[3] =  “student_three”;

   map<int, string>::iterator  iter;

   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明

mapStudent.insert(map<int, string>::value_type (1, “student_one”));

mapStudent.insert(map<int, string>::value_type (1, “student_two”));

上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下

Pair<map<int, string>::iterator, bool> Insert_Pair;

Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));

我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。

下面给出完成代码,演示插入成功与否问题

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

Pair<map<int, string>::iterator, bool> Insert_Pair;

   Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));

   If(Insert_Pair.second == true)

   {

          Cout<<”Insert Successfully”<<endl;

   }

   Else

   {

          Cout<<”Insert Failure”<<endl;

   }

   Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_two”));

   If(Insert_Pair.second == true)

   {

          Cout<<”Insert Successfully”<<endl;

   }

   Else

   {

          Cout<<”Insert Failure”<<endl;

   }

   map<int, string>::iterator  iter;

   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

大家可以用如下程序,看下用数组插入在数据覆盖上的效果

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent[1] =  “student_one”;

   mapStudent[1] =  “student_two”;

   mapStudent[2] =  “student_three”;

   map<int, string>::iterator  iter;

   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

map的大小



在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:


Int nSize = mapStudent.size();


4. ```
  数据的遍历

这里也提供三种方法,对map进行遍历

第一种:应用前向迭代器,上面举例程序中到处都是了,略过不表

第二种:应用反相迭代器,下面举例说明,要体会效果,请自个动手运行程序

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent.insert(pair<int, string>(1, “student_one”));

   mapStudent.insert(pair<int, string>(2, “student_two”));

   mapStudent.insert(pair<int, string>(3, “student_three”));

   map<int, string>::reverse_iterator  iter;

   for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)

{

   Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第三种:用数组方式,程序说明如下

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent.insert(pair<int, string>(1, “student_one”));

   mapStudent.insert(pair<int, string>(2, “student_two”));

   mapStudent.insert(pair<int, string>(3, “student_three”));

   int nSize = mapStudent.size()

//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)

//by rainfish

   for(int nIndex = 0; nIndex < nSize; nIndex++)

{

   Cout<<mapStudent[nIndex]<<end;

}

}

数据的查找(包括判定这个关键字是否在map中出现)



在这里我们将体会,map在数据插入时保证有序的好处。


要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。


这里给出三种数据查找方法


第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了


第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明


#include 


#include 


#include 


Using namespace std;


Int main()


{



Map<int, string> mapStudent;

mapStudent.insert(pair<int, string>(1, “student_one”));

mapStudent.insert(pair<int, string>(2, “student_two”));

mapStudent.insert(pair<int, string>(3, “student_three”));

map<int, string>::iterator iter;

iter = mapStudent.find(1);


if(iter != mapStudent.end())


{



Cout<<”Find, the value is ”<second<<endl;


}


Else


{



Cout<<”Do not Find”<<endl;


}


}


第三种:这个方法用来判定数据是否出现,是显得笨了点,但是,我打算在这里讲解


Lower\_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)


Upper\_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)


例如:map中已经插入了1,2,3,4的话,如果lower\_bound(2)的话,返回的2,而upper-bound(2)的话,返回的就是3


Equal\_range函数返回一个pair,pair里面第一个变量是Lower\_bound返回的迭代器,pair里面第二个迭代器是Upper\_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,程序说明


#include 


#include 


#include 


Using namespace std;


Int main()


{



Map<int, string> mapStudent;

mapStudent[1] = “student_one”;

mapStudent[3] = “student_three”;

mapStudent[5] = “student_five”;

map<int, string>::iterator iter;


iter = mapStudent.lower\_bound(2);




![img](https://img-blog.csdnimg.cn/img_convert/474145f5b5d9c8e8d67eb63b2e9be3d0.png)
![img](https://img-blog.csdnimg.cn/img_convert/666c3f86fd8e129aa1e8d4967d524f51.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

“student_three”));

   map<int, string>::iterator iter;

   iter = mapStudent.find(1);

if(iter != mapStudent.end())

{

   Cout<<”Find, the value is ”<<iter->second<<endl;

}

Else

{

   Cout<<”Do not Find”<<endl;

}

}

第三种:这个方法用来判定数据是否出现,是显得笨了点,但是,我打算在这里讲解

Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)

Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)

例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper-bound(2)的话,返回的就是3

Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,程序说明

#include

#include

#include

Using namespace std;

Int main()

{

   Map<int, string> mapStudent;

   mapStudent[1] =  “student_one”;

   mapStudent[3] =  “student_three”;

   mapStudent[5] =  “student_five”;

   map<int, string>::iterator  iter;

iter = mapStudent.lower_bound(2);

[外链图片转存中…(img-DyLeQ3JD-1715735391337)]
[外链图片转存中…(img-c4oTegii-1715735391338)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值