STL笔记(1)map

STL之map
ZZ from http://hi.baidu.com/liyanyang/blog/item/d5c87e1eb3ba06f41bd576cf.html

1.map中的元素其实就是一个pair.
2. map的键一般不能是指针, 比如int*, char*之类的, 会出错.
常用的就用string了,int也行.
3. map是个无序的容器, 而vector之类是有序的. 所谓有序无序是指放入的元素并不是按一定顺序放进去的,
而是乱序, 随机存放的(被映射后近似随机存放).所以遍历的时候有些效率差别.
4. 判断有没有找到该键的内容可以这样: 
std::map<std::string,Record>::const_iterator cIter;
cIter = stdfile.m_map.find(s);
if (cIter == stdfile.m_map.end()) // 没找到就是指向END了   
{
m_vecMoreFile.push_back(s);
}
如果键的内容是指针的话, 应该用NULL指针也可以判断了.
5. 遍历:
std::map<std::string,Record>::iterator iter;
for (iter = m_map.begin(); iter != m_map.end(); iter++)
{
std::string s = iter->second.filename;
}
由于map内容可以相当一个PAIR,
那就简单了, 用iter->second就可以取得值了.

可顺便转个其它的几种用法: 
1 头文件
#include <map>

2 定义
map<string, int> my_Map;
或者是typedef map<string, int>
MY_MAP;
MY_MAP my_Map;

3 插入数据
(1) my_Map["a"] = 1;
(2) my_Map.insert(map<string, int>::value_type("b",2));
(3) my_Map.insert(pair<string,int>("c",3));
(4) my_Map.insert(make_pair("d",4));

4 查找数据和修改数据
(1) int i = my_Map["a"];
my_Map["a"] = i;
(2) MY_MAP::iterator my_Itr;
my_Itr.find("b");
int j = my_Itr->second;
my_Itr->second = j;
不过注意,键本身是不能被修改的,除非删除。

5 删除数据
(1) my_Map.erase(my_Itr);
(2) my_Map.erase("c");
还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。

6 迭代数据
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}

7 其它方法
my_Map.size() 返回元素数目
my_Map.empty() 判断是否为空
my_Map.clear() 清空所有元素
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等

遍历:

#include<iostream>
#include <map>
using namespace std;

int main(){
 map<int,int>M;
 M[1]=2;
 M[2]=3;
 map<int,int>::iterator iter;
 for (iter = M.begin(); iter != M.end(); iter++)
 {
  cout<<iter->first<<" "<<iter->second<<endl;
 }
 return 0;
}

一个综合例子:

// WordCpp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    map <string, int> wcmap;
    for(int i=0;i<129;i++)
    {
        char word[100];
        sprintf(word,"word%d",i%10);
        wcmap.insert(make_pair((string)word, 0)).first->second ++;
    }

// output
    for(map<string, int>::iterator it = wcmap.begin(); it != wcmap.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }
    return 0;
}

STL笔记(1)map的更多相关文章

  1. STL笔记(5)条款49:学习破解有关STL的编译器诊断信息

    STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v( ...

  2. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  3. STL笔记(3) copy()之绝版应用

    STL笔记(3) copy()之绝版应用 我选用了一个稍稍复杂一点的例子,它的大致功能是:从标准输入设备(一般是键盘)读入一些整型数据,然后对它们进行排序,最终将结果输出到标准输出设备(一般是显示器屏 ...

  4. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  5. STL笔记(4)关于erase,remove

    STL笔记(4)关于erase,remove 你要erase的元素很容易识别.它们是从区间的“新逻辑终点”开始持续到区间真的终点的原来区间的元素.要除去那些元素,你要做的所有事情就是用那两个迭代器调用 ...

  6. STL中的map、unordered_map、hash_map

    转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...

  7. Effective STL 笔记 -- Item 6 ~ 7: Container and Object Pointer

    Effective STL 笔记 – Item 6 ~ 7: Container and Object Pointer 中间两次笔记被删掉了,简单补一下: Item 3 中提到如果将对象直接放入容器中 ...

  8. [知识点]C++中STL容器之map

    UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...

  9. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

随机推荐

  1. short-path problem (Floyd) 分类: ACM TYPE 2014-09-01 23:58 100人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  2. org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Server returned error code = 404 for URI.. Check server logs for details

    严重: Servlet.service() for servlet jsp threw exceptionorg.codehaus.xfire.XFireRuntimeException: Could ...

  3. 7 linux服务器程序规范

    1. Linux服务器程序一般以后台进程形式运行.后台进程又称守护进程(daemon),它没有控制终端,因而不会意外接收到用户输入.父进程通常为init(PID为1的进程)2. Linux服务器程序常 ...

  4. 使用Visual Studio 2012 开发 Html5 应用

    Visual Studio 一直以来是开发微软旗下应用的利器,只要是开发微软相关的应用无论是Windows程序,WPF,Asp.Net,WinRT Surface,WindowsPhone 等微软旗下 ...

  5. Unity3D之Assetbundle

    原地址: Unity3D之Assetbundle 有几个地方需要注意下 1.如何解决资源重复加载的问题 2.初始化了就直接出现在场景中了  感觉怪怪的 3.标红的地方要注意下  prefab上挂载的脚 ...

  6. 【C语言】二维数组做形参

    二维数组有两种形式: ①在栈上:         int a[4][4] = {...}; ②在堆堆上:          int ** a = new int *[4];           for ...

  7. UITableView多选删除

    设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...

  8. java获取系统当前时间

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式 System.out.print(df. ...

  9. cocos2d-x3.0环境搭建(基于win7以及mac)

    流程概览: Windows平台 一.安装 Python与配置Python环境变量 二.安装Cocos2d-x,并创建项目 Mac平台 安装Cocos2d-x,并创建项目 具体操作: 一.安装Pytho ...

  10. YARN学习笔记 ResourceManager部分

    CompositeService 多个service封装,service定义了状态机状态改变的合法情况. 重要的方法是(子类需要实现的):serviceStart,serviceInit,servic ...