STL--map用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力由于这个特性它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:
map<int, string> mymap;

1.数据的插入
(1)用insert函数插入value_type数据,下面举例说明
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap.insert(map<int,string>::value_type(1,"student_one"));
       mymap.insert(map<int,string>::value_type(2,"student_two"));
       mymap.insert(map<int,string>::value_type(3,"student_three"));
       map<int,string>::iterator it;
       for(it= mymap.begin();it!=mymap.end();it++)
        {
                cout << it->first << " " << it->second << endl;
        }
}

(2)用数组方式插入数据,下面举例说明
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap[1]="student_one";
       mymap[2]="student_two";
       mymap[3]="student_three";
       map<int,string>::iterator it;
       for(it=mymap.begin();it!= mymap.end();it++)
        {
            cout<<it->first<<" "<<it->second<< endl;
        }
}

以上两种用法,虽然都可以实现数据的插入,但是它们是有区别的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
mymap.insert(map<int, string>::value_type (1, "student_one"));
mymap.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 = mymap.insert(map<int, string>::value_type (1, "student_one"));
 
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
 
下面给出完成代码,演示插入成功与否问题
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       pair<map<int,string>::iterator,bool> Insert_pair;
       Insert_pair=mymap.insert(pair<int, string>(1,"student_one"));
       if(Insert_pair.second==true)
       {
               cout << "Insert Successfully" << endl;
       }
       else    cout << "Insert Failure" << endl;
       Insert_pair=mymap.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 it;
       for(it = mymap.begin(); it != mymap.end(); it++)
        {
            cout << it->first << " " << it->second << endl;
        }
}
运行结果:
Insert Successfully
Insert Failure
1  student_one

那么我们可以用如下程序,看下用数组插入在数据覆盖上的效果
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap[1]= "student_one";
       mymap[1]= "student_two";
       mymap[2]= "student_three";
       map<int,string>::iterator it;
       for(it=mymap.begin();it!=mymap.end();it++)
        {
            cout << it->first << " " << it->second << endl;
        }
        return 0;
}
运行结果:
1  student_two
2  student_three

2.   map的大小
在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:
Int nsize = mymap;.size();

3.  数据的遍历
这里提供三种方法,对map进行遍历
第一种:应用前向迭代器,上面举例程序中到处都是了,略过不表
第二种:应用反相迭代器,下面举例说明,要体会效果,以下为运行程序
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap.insert(map<int,string>::value_type(1,"student_one"));
       mymap.insert(map<int,string>::value_type(2,"student_two"));
       mymap.insert(map<int,string>::value_type(3,"student_three"));
       map<int,string>::reverse_iterator it;
       for(it=mymap.rbegin();it!=mymap.rend();it++)
        {
            cout << it->first << " "<< it->second << endl;
        }
}
运行结果:
3  student_three
2  student_two
1  student_one

第三种:用数组方式遍历,程序说明如下
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap.insert(map<int,string>::value_type(1,"student_one"));
       mymap.insert(map<int,string>::value_type(2,"student_two"));
       mymap.insert(map<int,string>::value_type(3,"student_three"));
       int nsize = mymap.size();
       for(int i=1;i<= nsize;i++)
        {
            cout << mymap[i] << endl;
        }
}
运行结果:
1  student_one
2  student_two
3  student_three

4. 数据的查找(包括判定这个关键字是否在map中出现)
在这里我们将体会,map在数据插入时保证有序的好处。
要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。
 
这里给出三种数据查找方法
第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap.insert(map<int,string>::value_type(1,"student_one"));
       mymap.insert(map<int,string>::value_type(2,"student_two"));
       mymap.insert(map<int,string>::value_type(3,"student_three"));
       map<int,string>::iterator it;
       it=mymap.find(1);
       if(it!= mymap.end())
        {
       cout<< "Find, the value is " << it->second << endl;
        }
        else
        {
            cout << "Do not Find" << endl;
        }
}
运行结果:
Find, the value is  student_one

第三种:这个方法用来判定数据是否出现,是显得笨了点,但是,我打算在这里讲解
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中不出现这个关键字
(这个暂时还没弄清楚。。。。。)

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

6.  数据的删除
这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
       map<int,string> mymap;
       mymap.insert(map<int,string>::value_type(1,"student_one"));
       mymap.insert(map<int,string>::value_type(2,"student_two"));
       mymap.insert(map<int,string>::value_type(3,"student_three"));
//如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好
 
       //如果要删除1,用迭代器删除
       map<int, string>::iterator it;
       it = mymap.find(1);
       mymap.erase(iter);
 
 
       //如果要删除1,用关键字删除
       int n = mymap.erase(1);//如果删除了会返回1,否则返回0
 
 
       //用迭代器,成片的删除
       //一下代码把整个map清空
       mymap.earse(mymap.begin(), mymap.end());
       //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
}

其余的STL—map功能暂不研究。

STL--map用法的更多相关文章

  1. c++ STL map 用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  2. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  3. STL map用法总结(multimap)

    2017-08-19 10:58:52 writer;pprp #include <map> #include <string> #include <iostream&g ...

  4. STL:map用法总结

    一:介绍 map是STL的关联式容器,以key-value的形式存储,以红黑树(平衡二叉查找树)作为底层数据结构,对数据有自动排序的功能.命名空间为std,所属头文件<map> 二:常用操 ...

  5. POJ2503 STL map用法

    2017-08-21 15:42:01 writer:pprp 除了用到map以外,输入也是一个问题 用到了sscanf详情请看上一篇博客 /* theme:第一章 - 分治算法 name: POJ ...

  6. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  7. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  8. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  9. 详解C++ STL map 容器

    详解C++ STL map 容器 本篇随笔简单讲解一下\(C++STL\)中的\(map\)容器的使用方法和使用技巧. map容器的概念 \(map\)的英语释义是"地图",但\( ...

  10. Features Track[STL map]

    目录 题目地址 题干 代码和解释 参考 题目地址 Features Track(ACM-ICPC 2018 徐州赛区网络预赛 ) 题干 代码和解释 题意:一个动画有许多 n 帧,每帧有 k 个点,点的 ...

随机推荐

  1. SPOJ-Grid ,水广搜easy bfs

    SERGRID - Grid 一个水广搜我竟然纠结了这么久,三天不练手生啊,况且我快三个月没练过搜索了... 题意:n*m的方格,每个格子有一个数,意味着在此方格上你可以上下左右移动s[x][y]个格 ...

  2. [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)

    传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...

  3. 刷题总结——bzoj1725(状压dp)

    题目: 题目描述 Farmer John 新买了一块长方形的牧场,这块牧场被划分成 N 行 M 列(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地. FJ  ...

  4. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  5. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  6. idea 自定义工具栏

    问题:在笔记本上面使用idea的时候,有时候,需要使用 Ctrl+Alt+左箭头 /  Ctrl+Alt+右箭头 跳转到 上一次,查看代码的问题,然而存在快捷冲突的时候,很蛋疼.下面是解决办法. 效果 ...

  7. Python入门--5--列表

    python没有数组 蛋是有列表 列表里面可以有:整数,浮点数,字符串,对象 没有数组,没有数组,没有数组,不重要的也说三遍!! 一.创建列表 x = ['abc','sas','www']     ...

  8. Python入门--4--分之和循环

    1.用ELIF比较省CPU: 第一种方法,使用if score = int(input('请输入你的分数:')) if (score <= 100) and (score >= 90): ...

  9. spring-boot-nginx代理-docker-compose部署

    在本地测试,使用docker部署不用在意环境 java测试项目: web框架:spring boot 框架 项目管理:maven 数据库:redis + postgres + mongo 部署相关:n ...

  10. MySQL与MSSQL的一些语法差异(持续更新中)

    分号不能少:分号不能少:分号不能少:重要的事情说3遍 Insert或者Update的数据包含反斜杠\的时候需要进行转义\\,例:insert into tablename(id,name) value ...