一、标准库的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. Bipolar transistor boosts switcher's current by 12 times

    The circuit in Figure 1 uses a minimal number of external parts to raise the maximum output current ...

  2. Windows Embedded Compact 7网络编程概述(下)

    11.1.1 Select I/O模型 在Windows CE中,Select模型是唯一被支持的I/O模型.Select I/O模型就是利用select函数对I/O进行管理. 函数select的功能在 ...

  3. MySQL MERGE存储引擎 简介

    MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...

  4. Meanshift算法

    [转载自Liqizhou],原文地址 Mean Shift算法,一般是指一个迭代的步骤,即先算出当前点的偏移均值,移动该点到其偏移均值,然后以此为新的起始点,继续移动,直到满足一定的条件结束. 1. ...

  5. 显示所有环境变量:env 或者 printenv

    显示所有环境变量:env 或者 printenv

  6. go语言基础之类型别名

    1.类型别名 示例: package main //必须有一个main包 import "fmt" func main() { //给int64起一个别名叫bigint type ...

  7. thinkphp问题

    这几天组里有个php系统报安全漏洞,负责的厂商跑了,没办法,被组长丢过来改漏洞,记录一下部分内容. 配置php的环境  参考https://blog.csdn.net/u011415782/artic ...

  8. JavaScript 你不知道的事 -- 关于函数

    接上篇Javascript 你不知道的事,直接条列了: 每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto__ ...

  9. MSSQL 触发器 暂停 和 启动

    开启关闭触发器 禁用: ALTER TABLE member DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE member ENABLE TRIGGER trig1 ...

  10. luigi框架--关于python运行spark程序

    首先,目标是写个python脚本,跑spark程序来统计hdfs中的一些数据.参考了别人的代码,故用了luigi框架. 至于luigi的原理 底层的一些东西Google就好.本文主要就是聚焦快速使用, ...