c++中的map是关联容器,以key-value的形式存储数据,类似于java中的Map类型和python中的dict。key不能重复,并能通过下标的形式(map[key])返回value,因此也被称为关联数组。注意:使用下标的形式赋值时会多一次初始化的操作,举个例子:map<string,int>,map["alex"] = 1,首先会检查alex这个key是否存在,如果存在则更新key对应的值为1,如果不存在则插入一个key,然后初始化其对应的value为0,最后更新其value为1.
下面根据一个简单的单词转换程序来学习map遍历、插入、删除、更新元素的操作。
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using namespace std;
//打开文件
ifstream& open_file(ifstream &in,const string &file) {
in.close();
in.clear();
in.open(file.c_str());
return in;
}
//遍历map
void traversalMap(const map<string,string> &transMap) {
map<string,string>::const_iterator tempMapIt = transMap.begin();
while(tempMapIt != transMap.end()) {
cout << tempMapIt->first << "--" << tempMapIt->second << endl;
++ tempMapIt;
}
cout << "*****************************************" << endl;
}
int main(int argc,char **argv) {
if(argc != 3)
throw runtime_error("arguments error");
map<string,string> transMap;
string key,value;
ifstream mapFile;
if(!open_file(mapFile,argv[1]))
throw runtime_error("open file error");
//插入数据
while(mapFile >> key >> value) {
transMap.insert(make_pair(key,value));
}
traversalMap(transMap);
ifstream input;
if(!open_file(input,argv[2]))
throw runtime_error("open file error");
string line;
//读取一行字符串
while(getline(input,line)) {
istringstream stream(line);
string word;
bool firstWorld = true;
//从一行字符串中读取一个单词
while(stream >> word) {
//查找word是否存在于map中
map<string,string>::const_iterator mapIt = transMap.find(word);
if(mapIt != transMap.end())
word = mapIt->second;
if(firstWorld)
firstWorld = false;
else
cout << " ";
cout << word;
}
cout << endl;
}
//更新key=f的值
transMap["f"] = "alexzhou";
traversalMap(transMap);
//删除key=f对应的元素
transMap.erase("f");
traversalMap(transMap);
//给key=f赋值,此时key不存在
transMap["f"] = "world";
traversalMap(transMap);
return 0;
}
这个程序我是在vs2010里运行的,需要设置一下运行时的参数,在项目属性里面 配置属性-》调试-》命令参数 里面写上你的参数,如:e:\\map.txt e:\\input.txt 。或者在cmd执行该项目生成的exe文件:StudyMap.exe e:\\map.txt e:\\input.txt
map.txt的文件内容为:
a my
b name
c is
d alexzhou
e hello
f world
input.txt文件的内容:
a b c d
e f
转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/c-c/619.html
分享到:
相关推荐
2024届求职-C++后端-学习笔记-操作系统、计算机网络、C++语言+算法 2024届求职-C++后端-学习笔记-操作系统、计算机网络、C++语言+算法 2024届求职-C++后端-学习笔记-操作系统、计算机网络、C++语言+算法 2024届求职-...
C++学习笔记--函数指针 函数指针 程序运行期间。每个函数都会占用一段连续的内存空间。而函数名就是该函数所占内存区域的起始地址(也称“入口地址”)。我们可以将函数入口地址赋给一个指针变量,使该指针变量...
C++11学习笔记 - 字串数据类型希望你觉得有用
C++非常实用-学习-总结-笔记-.pdf
C++ stl map algorithm ppt tutorial
Visual C++程序设计--基础与实例分析
VS2013调试,可以直接运行。由几个例子组成,备份留作以后查看。
C++学习笔记,博主个人向。内容是C++面对对象的,目录大致来源于菜鸟文档的C++面向对象,笔记是参考各种文章以及菜鸟文档改编的,内容包含C++类和对象(类定义和对象定义,类成员函数,类访问修饰符,友元函数,内联...
C++笔记 --- 类与结构 博客园大牛
UE4学习笔记----使用C++之控制球体运动并使用移动粒子效果(源代码)
UE4学习笔记----使用C++之玩家控制Pawn(源代码)
UE4学习笔记----使用C++之平滑切换相机(源代码)
UE4学习笔记----使用C++之快速入门(二)源代码
Visual C++程序设计 --基础与实例分析电子教案
C++语言源程序---背单词.zip
宽字节和多字节转换,利用C++实现UTF-8和UTF-16之间的转换,包括错误处理
包含所有core C++ 所有语法和技术点讲解,比较形象,文档整理的很好,希望对大家学习C++有帮助
c++学习笔记/知识要点精华版
warning: gcc-c++-4.4.7-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY error: Failed dependencies: gcc = 4.4.7-3.el6 is needed by gcc-c++-4.4.7-3.el6.x86_64 libstdc++ = 4.4.7-...