一、标准库的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. Control an LM317T with a PWM signal

    http://www.edn.com/design/analog/4363990/Control-an-LM317T-with-a-PWM-signal The LM317T from Nationa ...

  2. jdk8 流操作

    二.流 2.1 流介绍 流是Java API的新成员,它允许你以声明性方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现).就现在来说,你可以把它们看成遍历数据集的高级迭代器.此外,流还可以 ...

  3. 关于QtCharts中的映射器与模型的使用

    简述 本文章基于博主在使用QtCharts中一些经验总结,相关了Qt类有QVXYModelMapper,CustomTableModel(一个继承了QAbstractTableModel的类,用于实现 ...

  4. leetcode mock Shuffle an Array

    1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap ...

  5. Informatica 常用组件Source Qualifier之四 SQL Query

    源限定符转换提供 SQL 查询选项以覆盖默认的查询.您可以输入您的源数据库支持的 SQL 语句.输入查询之前,请连接您要在映射中使用的所有输入和输出端口. 编辑 SQL 查询时,您可以生成并编辑默认查 ...

  6. JS 与Flex交互:html中的js 与flex中的actionScript通信

    Flex与JavaScript交互的问题,这里和大家分享一下,主要包括Flex调用JavaScript中的函数和JavaScript调用Flex中的函数两大部分内容. Flex 与JavaScript ...

  7. STM32F103 GU906B模块GPRS、短信收发、拨号等功能的实现

    这个程序搞了我很久,尤其是对如何提高响应速度上,程序流程很简单,大概就是: 发送AT指令->等待模块响应->一旦响应了,立即返回,并处理掉. 这个程序不一定只能用在GU906上,程序框架在 ...

  8. 小议使用“完整”的CSS的缺点

    1.浏览器支持的不一致性 浏览器的漏洞或缺乏支持的CSS功能,导致不同的浏览器显示出不同的CSS版面编排.例如在微软Internet Explorer6.0的旧版本 ,执行了许多自己的CSS2.0属性 ...

  9. [C#.NET] X509 數位電子簽章

    摘自: http://www.dotblogs.com.tw/yc421206/archive/2012/06/30/73140.aspx 在上篇[C#.NET] 字串及檔案,利用 RSA 演算法加解 ...

  10. [React] Safely setState on a Mounted React Component through the useEffect Hook

    In the class version of this component, we had a method called safeSetState which would check whethe ...