STl容器(map)

map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力。在map容器内部,所有数据都是有序的。

比如一个班级的学生的学号和姓名存在一一映射的关系,用map可以轻易描述,(这里可以变相理解结构体,但比结构体简单的多)

map<int, string> student;
map<string ,int > student;

两者不同;前者容器中只存在唯一的int,而后者存在唯一的string;

1.map的构造函数

map<int, string> student;

2.数据的插入

这里讲二种(我会用到的)插入数据的方法:

第一种:用insert函数插入pair数据(此方法如果map容器中有此关键字,则就不能插入数据


#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string>student;//pair<int,string>p;p=make_pair(v1,v2); 
       student.insert(pair<int, string>(1, "student_one"));
       student.insert(pair<int, string>(2, "student_two"));
       student.insert(pair<int, string>(3, "student_three"));
       map<int, string>::iterator  iter;
       for(iter = student.begin(); iter != student.end(); iter++)//输出
       {
          cout<<iter->first<<"  "<<iter->second<<endl;
       }
}
make_pair()//返回类型为对应的pair类型
无需写出类别,就可以生成一个pair对象
例:
make_pair(1,'@')
而不必费力的写成
pair<int ,char>(1,'@')

用数组方式插入数据(如果map中存在此关键词,可以插入,但数据会覆盖之前的值

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string> Student;
       Student[1] =  "student_one";
       Student[2] =  "student_two";
       Student[3] =  "student_three";
       <int, string>::iterator  iter;
       for(iter = Student.begin(); iter != Student.end(); iter++)
       {
          cout<<iter->first<<"   "<<iter->second<<endl;
       }
}

3.map的大小

在往map里面插入了数据,可以用size函数,用法如下:

int nSize=student.size();

4.map遍历

扫描二维码关注公众号,回复: 2277849 查看本文章

正序遍历见上面,反向遍历如下


#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string> student;
       student.insert(pair<int, string>(1, "student_one"));
       student.insert(pair<int, string>(2, "student_two"));
       student.insert(pair<int, string>(3, "student_three"));
       map<int, string>::reverse_iterator  iter;
       for(iter = student.rbegin(); iter != student.rend(); iter++)//反向遍历
       {
          cout<<iter->first<<"   "<<iter->second<<endl;
       }

数组遍历


#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string>student;//pair<int,string>p;p=make_pair(v1,v2); 
       student.insert(pair<int, string>(1, "student_one"));
       student.insert(pair<int, string>(2, "student_two"));
       student.insert(pair<int, string>(3, "student_three"));
       for(int i=0;i<student.size() ;i++)
       {
          cout<<student[i]<<endl;
       }

}

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

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

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

这里给出两种(自己应该常用)数据查找方法

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

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

#include <map>
#include <string>
#include <iostream>
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 "<<iter->second<<endl;
       }
       else
       {
           cout<<"Do not Find"<<endl;
       }
}

6.  数据的清空与判空

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map

7.数据的删除

#include <map>
#include <string>
#include <iostream>
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"));
       //如果要删除1,用迭代器删除
       map<int, string>::iterator iter;
       iter = mapStudent.find(1);
       mapStudent.erase(iter);
       for(int i=0;i<mapStudent.size() ;i++)
       {
          cout<<mapStudent[i]<<endl;
       }
}

现阶段常用功能大概就这些了,以后会更新其他功能

参考https://blog.csdn.net/sunshinewave/article/details/8067862

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81132714