c++学习笔记-map的使用实例(单词转换)
2012-09-26 00:36
543 查看
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遍历、插入、删除、更新元素的操作。
这个程序我是在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
下面根据一个简单的单词转换程序来学习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
相关文章推荐
- c++学习笔记——个单词转换的map程序详解
- c++学习笔记——个单词转换的map程序详解
- 【VS2010学习笔记】【编程实例】 (在Visual Studio中使用C++创建和使用DLL)
- 使用map实现单词转换的实例分析
- 【VS2010学习笔记】【编程实例】 (在Visual Studio中使用C++创建和使用DLL)
- C++学习笔记——map与set的组合使用
- 【C++学习笔记】一个使用new和delete的实例
- c++学习笔记:使用锋利的sstringstream完成类型转换
- Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
- C++学习笔记(六):关于ifdef 和ifndef的使用
- C++学习笔记--STL(映射:map)
- 【C++】学习笔记草稿版12(类对象的类型转换)
- 使用c++开发学习笔记
- [Erlang 学习笔记] 使用 rebar 创建 application(basho- lager 应用实例)
- Java写的手机计费系统(作为学习参考的实例:关键是Java类库里面的date和Calendar的使用和相互转换,以及一些日期的实际问题)
- 【C++】学习小笔记系列-4-何时使用引用参数
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
- C++ 学习笔记(4)表达式、运算符、类型转换(static_cast、const_cast、reinterpret_cast、dynamic_cast)
- 1.JAVA中使用JNI调用C++代码学习笔记 - 星云博客园
- C++学习笔记之map键值对的修改