一、相关定义

map

  • 关联容器,存储相结合形成的一个关键值和映射值的元素
  • 提供一对一(第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可以称为该关键字的值)的数据处理能力
  • map对象是模板类,需要关键字和存储对象两个模板参数

特征

  • Map 是一种Pair Associative Container,意味着它的值类型为 pair<const Key, Data>,而且也是 Unique Associative Container,也就是任何两个元素没有相同的key值;
  • map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的;
  • 在map对象中插入一个新元素不指向现有元素的迭代器失效。从map上删除一个元素,也没有任何迭代器失效,除非,当然,实际上指向正在被删除的元素的迭代器;
  • 增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响;
  • map主要建立了key到value的映射。对于迭代器来说,可以修改实值,而不能修改key。

数据映射

举例说明:比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可以轻易描述,很明显学号用int描述,姓名用字符串描述

给出map描述代码:map<int,string> mapStudent;

二、map

【构造方法】

  • map<key,value> myMap;  //为了使用方便,可以对模板类进行一下类型定义  typedef map<int, string> myMap; myMAP enumMap;
  • map<key,value>::iterator iter;  //迭代器iter

【功能】

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

【常用方法】

insert方法

在map中插入一个元素,map中记录的元素通常为键值对,所以,在存储时会把键和值封装成pair然后进行插入,例如:phone.insert(pair<string,string>(name,number));其中name和number为string类型的变量。当然也可以简单的写成phone[name]=number; 此处phone即为map<string,string>类型的变量。因为map在实现过程中对[]进行了重载。

第一种方式若插入的元素的键值已经存在于map中,那么就会插入失败,不会修改元素的键值对信息,若键值在map中查找不到,那么就会将该新元素加入到map中去。

第二种方式比较直观,但存在一个性能的问题。插入2时,先在phone中查找主键为name的项,没发现,然后将一个新的对象插入phone,键是name,值是一个空字符串,插入完成后,将字符串赋为number, 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。若找到键值为name的项,则用number更改原来的number值。

erease方法

erease主要是删除map中的某个项,需要参数key,例如phone.erease(name);此句的意思就是删除key值为name的键值对。

size方法

统计map中键值对的个数,phone.size()返回值即为phone中键值对的个数,若map为空则返回0

count方法

统计map中某个键值出现的次数,因为map中键值唯一,所以此方法可以用来检测某键值是否存在,例如在删除时可以phone.count(name),若为0则可以提示用户此键值不存在,若为1则直接删除。Ps:erease无论要删除的键值对是否存在都能正常执行。

begin/end方法

begin方法返回map迭代器类型,通过此迭代器与end方法的返回值进行比较就可以很容易的对map进行遍历。

clear方法

清空map中的所有元素

empty方法

判断map是否为空,若为空则返回真若非空则返回假。

Ps:由于map中存储的是键值对,迭代器为ite,则ite->first为key,ite->second为值

【代码一】

#include <iostream>
#include <map>
#include <string> 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(){
char tmp[32]="";
map<string, string> mapS;
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 there elements"<<endl;
cout<<"----------------------"<<endl; iter=mapS.find("192.168.200.33"); //查找
cout<<"查找"<<endl;
if(iter!=mapS.end()){
cout<<"find the element"<<endl;
cout<<"it is:"<<iter->second<<endl;
}else{
cout<<"not find the element"<<endl;
} //遍历
cout<<"遍历"<<endl;
for(iter=mapS.begin();iter!=mapS.end();iter++){
cout<<"| "<<iter->first<<" | "<<iter->second<<" |"<<endl;
} //删除
cout<<"删除"<<endl;
iter=mapS.find("192.168.200.33");
if(iter!=mapS.end()){
cout<<"find the element"<<endl;
cout<<"delete the element"<<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;
}
return 0;
}

【代码二】

#include <iostream>
#include <map>
#include <cstring>
#include <stdlib.h>
#include <fstream> using namespace std; class PhoneBook
{
public:
PhoneBook() {}
~PhoneBook() {}
int printall();
int input(string name,string number);
string look(string name);
void exitt();
int readfile(const string filename);
int writefile(const string filename);
int cnt(); private:
map<string,string> phone;
}; int PhoneBook::printall()
{
map<string,string>::iterator ite = phone.begin();
while(ite!=phone.end()) {
cout<<ite->first<<"\t";
cout<<ite->second<<endl;
ite++;
}
return 0;
} string PhoneBook::look(string name){
map<string,string>::iterator ite = phone.find(name);
if(ite == phone.end()){
return "No this user";
}else{
return ite->first+"\t\t"+ite->second;
}
} void PhoneBook::exitt()
{
exit(0);
} int PhoneBook::readfile(const string filename)
{
string a,b,c;
fstream fd;
fd.open(filename.c_str(),ios::in|ios::binary);
if(!fd) {
cout<<"error:write file!"<<endl;
exit(1);
}
phone.clear();
while(fd>>a>>b>>c){
phone.insert(pair<string,string>(a,b));
}
fd.close();
return 0; } int PhoneBook::writefile(const string filename)
{
fstream fd(filename.c_str(),ios::out|ios::binary);
map<string,string>::iterator ite = phone.begin();
while(ite!=phone.end()){
fd<<ite->first<<" "<<ite->second<<" | ";
ite++;
}
fd.close();
return 0;
} int PhoneBook::input(string name,string number)
{
//phone[name]=number;
phone.insert(pair<string,string>(name,number));
return 0;
} int PhoneBook::cnt(){
return phone.size();
} int main()
{
PhoneBook *book = new PhoneBook();
char command[40]; while(true){
cin>>command;
if(strcmp(command,"insert")==0){
string name,number;
cin>>name>>number;
book->input(name,number);
}else if(strcmp(command,"print")==0){
book->printall();
}else if(strcmp(command,"search")==0){
string name;
cin>>name;
cout<<book->look(name)<<endl;
}else if(strcmp(command,"load")==0){
book->readfile("d:\\12.txt");
}else if(strcmp(command,"upload")==0){
book->writefile("d:\\12.txt");
}else if(strcmp(command,"exit")==0){
book->exitt();
}else if(strcmp(command,"count")==0){
cout<<book->cnt()<<endl;
}else{
cout<<"Command Error!\n";
continue;
}
}
return 0;
}

C++STL——map的更多相关文章

  1. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  2. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  3. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  4. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  5. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  6. STL MAP 反序迭代

    ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...

  7. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  8. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

  9. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

  10. C++ STL map使用

    Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map构造函数:map<string , in ...

随机推荐

  1. 课时89.CSS三大特性练习(理解)

    给大家补充一点:通配符是不参与权重计算的,因为通配符的权重是0,而权重只计算id,类,标签,将来还有一种,到后面说. 练习1 直接选中和间接选中的,必然要听直接选中的. 练习2 直接选中一共有两个,直 ...

  2. 菜鸟笔记 -- Chapter 11 格式化

    我们在String中介绍过它有一个格式化的方法,在其它很多地方,也都能看到格式化的操作,那么这节我们就来认真了解一下Java中的格式化操作. 我们在操作中涉及到的格式化有字符串的格式化和一些其它数据类 ...

  3. Android手机上抓包神器

    Packet Capture 一款依托安卓系统自身VPN来达到免Root抓取数据包的应用程序.Packet Capture一个使用SSL网络解密的 捕获数据包/网络嗅探 工具,虽然它的功能并不丰富,但 ...

  4. sqlServer2014安装说明(windows7 64位)

    SqlServer2014安装说明(windows7 64位) 地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=42299 1, ...

  5. 设置Oracle数据库开机自启动

    1.oracle 用户下 修改$ORACLE_HOME/bin/dbstart   vim /home/oracle/database/product/12c/db_1/bin/dbstart 将OR ...

  6. WebGL学习笔记(3)

    根据上篇笔记,在对3D对象可进行普通的控制后,以及学习了http://hiwebgl.com的教程第10章内容:世界模型的载入以及控制镜头移动,经过多次调试矩阵代码,已经可以实现在世界中旋转镜头/控制 ...

  7. jQuery 切换图片(图标)效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【cisco下针对冗余链路故障备份的处理措施】

    对于中小型的网络中,为了流量的分担,可制定负载均衡方案,但往往带来的是链路的冗余.导致多条物理线路不能够最大的发挥其作用:冗余链路随可避免环路,但在实际的网络中还是存在一些需要完善的地方: 假设有一组 ...

  9. Windows下安装Mysql5.5.27(社区版)

    所有平台的 MySQL 下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. 运行mysql-5.5.27-win32.msi 进入欢迎界面 ...

  10. ExceL按记录导出Txt 工具

    根据客户要求,开发此工具,每一条记录改出一个Txt文本,文本名取其中一字段数据