C++ map

1、map简介

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。

2、map的功能

  1. 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
  2. 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
  3. 快速插入Key - Value 记录。
  4. 快速删除记录
  5. 根据Key 修改value记录。
  6. 遍历所有记录。

3、使用map

使用map得包含map类所在的头文件

#include <map> //注意,STL头文件没有扩展名.h

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

std:map<int, string> personnel;

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

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

typedef map<int, CString> UDT_MAP_INT_CSTRING;

UDT_MAP_INT_CSTRING enumMap;

4、在map中插入元素

改变map中的条目非常简单,因为map类已经对[]操作符进行了重载

enumMap[1] = "One";

enumMap[2] = "Two";

.....

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

enumMap.insert(map<int, CString> :: value_type(2, "Two"))

5、查找并获取map中的元素

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

CString tmp = enumMap[2];

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

我们可以使用Find()和Count()方法来发现一个键是否存在。

查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.

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

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

UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);

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

//没找到

}

else {

//找到

}

通过map对象的方法获取的iterator数据类型是一个std::pair对象,包括两个数据 iterator->first 和 iterator->second 分别代表关键字和存储的数据

6、从map中删除元素

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

该成员方法的定义如下

  1. iterator erase(iterator it); //通过一个条目对象删除
  2. iterator erase(iterator first, iterator last); //删除一个范围
  3. size_type erase(const Key& key); //通过关键字删除

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

C++ STL map的使用

以下是对C++中STL map的插入,查找,遍历及删除的例子:

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

void map_insert(map < string, string > *mapStudent, string index, string x) 

mapStudent->insert(map < string, string >::value_type(index, x)); 


int main(int argc, char **argv) 

char tmp[32] = ""; 
map < string, string > mapS; 

//insert element 
map_insert(&mapS, "192.168.0.128", "xiong"); 
map_insert(&mapS, "192.168.200.3", "feng"); 
map_insert(&mapS, "192.168.200.33", "xiongfeng"); 

map < string, string >::iterator iter; 

cout << "We Have Third Element:" << endl; 
cout << "-----------------------------" << endl; 

//find element 
iter = mapS.find("192.168.0.33"); 
if (iter != mapS.end()) { 
cout << "find the elememt" << endl; 
cout << "It is:" << iter->second << endl; 
} else { 
cout << "not find the element" << endl; 


//see element 
for (iter = mapS.begin(); iter != mapS.end(); iter ) { 

cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl; 


cout << "-----------------------------" << endl; 

map_insert(&mapS, "192.168.30.23", "xf"); 

cout << "After We Insert One Element:" << endl; 
cout << "-----------------------------" << endl; 
for (iter = mapS.begin(); iter != mapS.end(); iter ) { 

cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl;



cout << "-----------------------------" << endl; 

//delete element 
iter = mapS.find("192.168.200.33"); 
if (iter != mapS.end()) { 
cout << "find the element:" << iter->first << endl; 
cout << "delete element:" << iter->first << endl; 
cout << "=================================" << endl; 
mapS.erase(iter); 
} else { 
cout << "not find the element" << endl; 

for (iter = mapS.begin(); iter != mapS.end(); iter ) { 

cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl; 


cout << "=================================" << endl; 

return 0; 
}

### C++ 中 `map` 容器的使用方法 #### 创建和初始化 `map` 在C++中,`map` 是标准模板库(STL)的一部分,用于存储键值对的数据结构。创建一个简单的整数到字符串映射可以如下所示: ```cpp #include <iostream> #include <map> using namespace std; // 初始化 map 的几种方式 void createAndInitializeMap() { // 方式一:通过下标操作符 [] map<int, string> stuMap; stuMap[1001] = "Jason"; // 方式二:使用 insert 方法配合 pair 或 make_pair 插入元素 stuMap.insert(pair<int, string>(1002, "Helen")); stuMap.insert(make_pair(1003, "Steve"))[^2]; } ``` #### 遍历 `map` 为了遍历整个 `map`,可以采用多种方式进行访问。这里展示如何利用范围for循环以及迭代器来实现这一点。 ```cpp // 使用范围 for 循环遍历 map void traverseWithRangeFor(map<int, string>& m) { for (auto& elem : m) { cout << "Key: " << elem.first << ", Value: " << elem.second << endl; } } // 使用迭代器遍历 map 并结合 STL 算法 like std::for_each void traverseWithIterator(map<int, string>& m) { auto callback = [](const pair<const int, string>& p) { cout << "Key: " << p.first << ", Value: " << p.second << endl; }; for_each(m.begin(), m.end(), callback)[^1]; } ``` #### 访问特定项 当需要获取某个具体位置上的值时,可以直接通过键名索引或者find函数查找对应的记录。 ```cpp string getValueByKey(const map<int, string>& m, const int key) { if (m.find(key) != m.cend()) { return (*m.find(key)).second; } else { throw runtime_error("Key not found"); } } ``` 上述代码片段展示了基本的操作模式,包括但不限于定义、填充、检索和遍历关联数组型容器——即 `map`。这些功能使得处理具有唯一性的键与其相关联的信息变得简单而高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值