一、标准库的map类型

使用map得包含map类所在的头文件

template <
class Key,
class Type,
class Traits = less<Key>,
class Allocator=allocator<pair <const Key, Type> >
>
class map

#include <map>

定义一个map对象: map<string, int> mapTest;

//用string作为索引,存储int对象

例程1:map 插入数据

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
    // 插入到map容器内部的元素默认是按照key从小到大来排序。
    // key类型一定要重载<运算符
    map<string, int> mapTest;

mapTest["aaa"] = 100;   // int& operator[](const string& index);
    mapTest["eee"] = 500;
    mapTest["eee"] = 300; //[]方式,key重复,则被修改成最后一次插入的值。
    mapTest.insert(map<string, int>::value_type("bbb", 200));
    mapTest.insert(map<string, int>::value_type("bbb", 2000)); //不允许key值重复插入,无效
    mapTest.insert(pair<string, int>("ccc", 300));
    mapTest.insert(pair<string, int>("ccc", 3000));
    mapTest.insert(make_pair("ddd", 400));
    mapTest.insert(make_pair("ddd", 4000));

map<string, int>::const_iterator it;
    for (it = mapTest.begin(); it != mapTest.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }

return 0;

}

如上所述,

例程2:map 查找与修改

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
    // 插入到map容器内部的无素默认是按照key从小到大来排序。
    // key类型一定要重载<运算符
    map<string, int> mapTest;

mapTest["aaa"] = 100;   // int& operator[](const string& index);
    mapTest.insert(map<string, int>::value_type("bbb", 200));
    mapTest.insert(pair<string, int>("ccc", 300));
    mapTest.insert(make_pair("ddd", 400));

int n = mapTest["bbb"];
    cout << n << endl;
    mapTest["bbb"] = 2000;

map<string, int>::iterator it;
    it = mapTest.find("ccc");
    if (it != mapTest.end())
    {
        it->second = 3000;
    }
    else
    {
        cout << "not found" << endl;
    }

//map<string,int>::const_iterator it;
    for (it = mapTest.begin(); it != mapTest.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }

return 0;

}

例程3:map 删除数据

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
    // 插入到map容器内部的无素默认是按照key从小到大来排序。
    // key类型一定要重载<运算符
    map<string, int> mapTest;

mapTest["aaa"] = 100;   // int& operator[](const string& index);
    mapTest.insert(map<string, int>::value_type("bbb", 200));
    mapTest.insert(pair<string, int>("ccc", 300));
    mapTest.insert(make_pair("ddd", 400));

mapTest.erase("bbb");
    map<string, int>::const_iterator it;
    it = mapTest.find("ccc");
    if (it != mapTest.end())
    {
        mapTest.erase(it);
    }

for (it = mapTest.begin(); it != mapTest.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }

return 0;

}

例程4:使用map 计算每个单词出现的次数

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
/*************************************************************************
> File Name: count_words.cpp
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 10 Nov 2012 10:20:14 AM CST
************************************************************************/

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

using namespace std;

int main(void)
{
    string s;
    map<string, int> counters;
    // read the input
    while (cin >> s)
        ++counters[s];
    cout << endl;
    for (map<string, int> :: const_iterator it = counters.begin();
            it != counters.end(); ++it)
    {

cout << it ->first << "\t" << it->second << endl;
    }
    return 0;
}

参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范

map 类简介和例程的更多相关文章

  1. vector 类简介和例程

    一.标准库的vector类型 vector是同一种类型的对象的集合 vector的数据结构很像数组,能非常高效和方便地访问单个元素 vector是一个类模板(class template) vecto ...

  2. string 类简介和例程

    一.标准库string类型 string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作 ,在VC中直接F1查看 template < class C ...

  3. ArcGIS API for JavaScript 入门教程[5] 再讲数据——Map类之底图与高程

    [回顾]前4篇交代了JsAPI的背景.资源如何获取,简介了数据与视图分离的概念与实现,剖析了页面的大骨架. 这篇开始,讲Map类. 转载注明出处,博客园/CSDN/B站/知乎:秋意正寒 目录:http ...

  4. Java Map 集合类简介

      作者:Jack Shirazi 了解最常用的集合类型之一 Map 的基础知识以及如何针对您应用程序特有的数据优化 Map. 本文相关下载: · Jack 的 HashMap 测试 · Oracle ...

  5. JAVA Map集合类简介

    了解最常用的集合类型之一 Map 的基础知识以及如何针对您应用程序特有的数据优化 Map. 本文相关下载: · Jack 的 HashMap 测试· Oracle JDeveloper 10g jav ...

  6. ImageView类简介

    4.8  图片控件 本节将要介绍的是图片控件ImageView,首先对ImageView类进行简单介绍,然后通过一个案例来说明ImageView的用法. 4.8.1  ImageView类简介 Ima ...

  7. 探究Java中Map类

    Map以按键/数值对的形式存储数据,和数组非常相似,在数组中存在的索引,它们本身也是对象.       Map的接口       Map---实现Map       Map.Entry--Map的内部 ...

  8. Spring Security——核心类简介——获得登录用户的相关信息

    核心类简介 目录 1.1     Authentication 1.2     SecurityContextHolder 1.3     AuthenticationManager和Authenti ...

  9. 关于 Go 中 Map 类型和 Slice 类型的传递

    关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...

随机推荐

  1. Maven:Generating Project in Batch mode 卡住问题

    Maven命令执行到Generating Project in Batch mode 卡住,原因是网络带宽不足问题!需要下载一个约4.1M的archetype-catalog.xml文件. Maven ...

  2. C# 通过HttpWebRequest在后台对WebService进行调用

    通过HttpWebRequest在后台对WebService进行调用 http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.ht ...

  3. JavaScript的学习要点

    概要 了解Javascript历史以及Javascript三个不同组成部分: ECMAScript DOM(文档对象模型) BOM(浏览器对象模型) ECMAScript 目标 掌握Javascrip ...

  4. 3D 坐标变换 公式 推导

    [ 更新 ]更好的方法见[用抽象代数讨论仿射变换和仿射空间中的坐标变换] ,以下是之前的内容. 以下的推导 结论是正确的,可是过程有点懵. 以下使用行向量: e1=(1,0,0) e2=(0,1,0) ...

  5. django单表操作 增 删 改 查

    一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...

  6. Qt Creator的安装与Qt交叉编译的配置

    Qt Creator 的安装 到Qt官网下载Qt Creator  https://www.qt.io/download-open-source/ 其它旧版本点击Achieve连接下载 或登录http ...

  7. windows安装Jupyter Notebook

    这是我自定义的Python 的安装目录 (D:\SoftWare\Python\Python36\Scripts) 1.Jupyter Notebook 和 pip 为了更加方便地写 Python 代 ...

  8. IOS中WebView的使用

    UIWebView是iOS sdk中一个最常用的控件.是内置的浏览器控件,我们可以用它来浏览网页.打开文档等等,UIWebView能够加载html/htm.pdf.docx.txt等格式的文件  系统 ...

  9. hdu 1007 Quoit Design(分治法求最近点对)

    大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...

  10. Trie树统计单词前缀

    输入 输入的第一行为一个正整数n.表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦).单词由不超过10个的小写英文字母组成,可能存在同样的单词.此时应将其视作不同的单 ...