一、标准库的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. [置顶] 解决Firefox/Opera 不支持 onselectstart事件

    在开发中,很多区域是不允许用户select的,在IE/Safari/Chrome中我们可以使用onselectstart事件来阻止用户选定元素内文本, 但在火狐中,这段区域还是可以选择的, 如下: & ...

  2. 在Mac中设置Ctrl+C/V进行复制/粘贴

    从Windows世界走入Mac世界,最让不习惯的是在Mac中“复制/粘贴”的快捷键是Command+C/V.而且Command键与C/V键靠得太近,只能用大拇指与食指进行操作,也让人不习惯.再加上远程 ...

  3. hashmap实现原理2

    put和get都首先会调用hashcode方法,去查找相关的key,当有冲突时,再调用equals(这也是为什么刚开始就重温hashcode和equals的原因)! HashMap基于hashing原 ...

  4. 仿LOL项目开发第一天

    ---恢复内容开始--- 仿LOL项目开发第一天 by---草帽 项目源码研究群:539117825 最近看了一个类似LOL的源码,颇有心得,所以今天呢,我们就来自己开发一个类似于LOL的游戏demo ...

  5. Eclipse中在android项目中出现新建一个Activity后,出现整个project的报错以及包导入以后无法执行等等情况分析。

    今天用Eclipse去写android项目,然后后面须要建一个Blank  Activity后,非常正常的建立的.然后那个Activity是基于ActionBarAtivity,要导入v7,结果由于这 ...

  6. 了解Redis 和 Memcached 的区别

    1.Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支持更丰富的数据操作,通常在Memcached里,你需要将数据拿到客户端来进行类似的修改再set回去. ...

  7. Android -- sqlite数据库随apk发布

    背景                                                                                            把在工程中测 ...

  8. 用php当作cat使用

    今天,本来是想敲 node test.js 执行一下,test.js文件,结果 惯性的敲成了 php  test.js, 原文输出了 test.js的内容. 突然觉得,这东西 感觉好像是 cat  命 ...

  9. Android安装包相关知识汇总 (编译过程图给力)

    转自: https://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=208008519&idx=1&sn=278b7793699 ...

  10. Linux 内存泄露小结

    本文仅限记录自己的一次 内存泄露追踪小记. 可能并不十分适用与大家的情况.而且方法也并不是很smart.仅做记录,能提供个思路更好.        一. 要问调试程序遇到什么问题最头疼, 内存泄露肯定 ...