std::map用法

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树AVL),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

使用map对象首先要包括头文件,包含语句中必须加入如下包含声明:

#include <map>

using namespace map;

1、map介绍

1、1 map的构造

定义的原型为: Template<class T1, class T2>

map(); // 默认构造函数

map(const map& m) // 拷贝构造函数

map(iterator begin, iterator end ); //区间构造函数

map(iterator begin, iterator end, const traits& _compare) //带比较谓词的构造函数

map(iterator begin, iterator end, const traits& _compare, const allocator& all) //带分配器

1、2 map定义

1)map的基本定义

map对象是模板类,需要关键字和存储对象两个模板参数:

std:map<int, string> personnel;

这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

为了使用方便,可以对模板类进行一下类型定义:

typedef map<int, CString> UDT_MAP_INT_CSTRING;

UDT_MAP_INT_CSTRING enumMap; //后面会依此例说明

1.2.2 map的嵌套定义

map<sring,map<string,long> > //注意:最后两个>之间有个空格

map支持下标运算符operator[],用访问普通数组的方式来访问map;不过下标为map的键,在multimap中一个键可以对应多个不同的值。

以下是map的一些定义:

map<int,string> maphai;

map<char,int> maphai;

map<string,char> mapstring;

map<string,int> mapstring;

map<int,char>mapint;

map<char,string>mapchar;

2、map的方法

2、1 在map中插入元素

三种插入方式:

typedef map<int, CString> UDT_MAP_INT_CSTRING;

以UDT_MAP_INT_CSTRING enumMap;为例:

1)用insert方法插入pair对象:

enumMap.insert(pair<int, Cstring>(1, “One”));

2)用insert方法插入value_type对象:

enumMap.insert(map<int, Cstring>::value_type (1, “One”));

3)用数组方式插入值:

enumMap[1] = "One";

enumMap[2] = "Two";

......

注:方法3)非常直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,然后将一个新的对象插入enumMap,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。用前两种方法可以避免开销。

2、2 查找并获取map中元素

1)下标操作符给出了获得一个值的最简单方法:

CString tmp = enumMap[2];

但是,只有当map中有这个键的实例时才对,否则会自动插入一个实例,值为初始化值。

2)我们可以使用find()方法来发现一个键是否存在

查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,没找到则指向end()。在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator。如我们在取key为2的元素前可加如下代码:

int nFindKey = 2; //要查找的Key

//定义一个条目变量(实际是指针)

map<int, CString>::iterator it = enumMap.find(nFindKey);

if(it == enumMap.end()) {

cout<<"没找到"<<endl;

}

else {

cout<<"找到了"<<endl;

}

通过map对象的方法获取的iterator数据类型是一个std::pair对象,包括两个数据。

iterator->first 关键字(key)

iterator->second 存储的数据(value)

2.3 从map中删除元素

1)移除某个map中某个条目用erase()

该成员方法的定义如下:

1.iterator erase(iterator it); //通过一个条目对象删除

2.iterator erase(iterator first, iterator last); //删除一个范围

3.size_type erase(const Key& key); //通过关键字删除

//正确的删除做法是:

for(map<int,string>::iterator it = str_map.begin(); it!=str_map.end(); )

{

if ( some_condition ) {

str_map.erase(it++);

} else {

it++;

}

}

2)清除所有的元素clear()

clear()就相当于 enumMap.erase(enumMap.begin(), enumMap.end());

2、4 map的遍历

1)通过迭代器遍历

std::map<std::string,Record>::iterator iter;

for (iter = m_map.begin(); iter != m_map.end(); iter++)

{

Ctring s = iter->second;//取键值

}

2)通过数组访问方式遍历

int nSize = mapStudent.size(); //获取元素个数

//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++);

for(int nIndex = 0; nIndex < nSize; nIndex++)

{

Cout<<mapStudent[nIndex]<<endl;

}

3、map的基本操作函数

C++ Maps是一种关联式容器,包含“关键字/值”对

begin() 返回指向map头部的迭代器

clear() 删除所有元素

count() 返回指定元素出现的次数

empty() 如果map为空则返回true

end() 返回指向map末尾的迭代器

equal_range() 返回特殊条目的迭代器对

erase() 删除一个元素

find() 查找一个元素

get_allocator() 返回map的配置器

insert() 插入元素

key_comp() 返回比较元素key的函数

lower_bound() 返回键值>=给定元素的第一个位置

max_size() 返回可以容纳的最大元素个数

rbegin() 返回一个指向map尾部的逆向迭代器

rend() 返回一个指向map头部的逆向迭代器

size() 返回map中元素的个数

swap() 交换两个map

upper_bound() 返回键值>给定元素的第一个位置

value_comp() 返回比较元素value的函数

3) map中swap的用法

map中的swap不是一个容器中的元素交换,而是两个容器交换;

map <int, int> m1, m2;

m1.swap( m2 );

4) map的sort问题

map中的元素是自动按key升序排序,所以不能对map用sort函数:

map的效率问题:还要说明的是,map中由于它内部有序,由红黑树保证,因此很多函数执行的时间复杂度都是log2N的,如果用map函数可以实现的功能,而STL Algorithm也可以完成该功能,建议用map自带函数,效率高一些。

map在空间上的特性:由于map的每个数据对应红黑树上的一个节点,这个节点在不保存你的数据时,是占用16个字节的,一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),我想大家应该知道,这些地方很费内存的。

时间: 2024-08-24 17:52:56

std::map用法的相关文章

C++ std::map::erase用法及其陷阱

1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string,string>::iterator ITER; ITER iter=mapTest.find(key); mapTest.erase(iter); 像上面这种删除单个节点,map的行为不会出现问题,但是当在一个循环里用的时候,往往会被误用. 2.陷阱 eg: for(ITER iter=mapTest

STL之std::set、std::map的lower_bound和upper_bound函数使用说明

由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊的函数,lower_bound.upper_bound.equal_range. 原型如下: iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) con

C++11 std::function用法(c++常问问题十七)

C++11 std::function用法 直接上代码: 例子1:std::function的感觉就像是函数指针那样有木有 #include <iostream> #include <functional> #include <map> using namespace std; // 普通函数 int add(int i, int j) { return i + j; } //lambda表达式 auto mod = [](int i, int j){return i

STL中的map用法详解

STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.map概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),

std::map使用结构体自定义键值

使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; mp[(Node){x,y}]++; 这样子的话,会出现一堆报错 c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_function.h||In instantiation of 'bool std::less<_Tp>::operator()(

c++ how to make your own class a valid key type for std::map?

In Java, if you want your own class to be a valid key type of the container, you just need to make it implement the interface "Comparable", and then it'll work properly. How about in C++? I was wondering whether C++ has offered some kind of mech

std::map常用方法

map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair("Peter Q.", 5328)); std::map常用方法,布布扣,bubuko.com

stl std::map容器排序及使用注意事项 .

01.#include "stdafx.h" 02.#include <map> 03.#include <iostream> 04. 05.int _tmain(int argc, _TCHAR* argv[]) 06.{ 07. /** 08. * map中的每个元素都是一个pair类型 09. * 对于插入其中的元素都会默认按键值升序排列好 10. */ 11. 12. std::map<int, int> m; 13. m.insert(st

RAD C++Builder xe7 std::map xtree BUG

c++Builder 6 下的std::map还能用,有代码提示. 换到xe7,代码提示出来就一个tt.operator [](),代码没法往下写了. 最后把Target Platforms切换到64 bit windows 竟然可以了!!!