STL模版库的vector,map的简单应用
来源:互联网 发布:软件图标消失 编辑:程序博客网 时间:2024/05/13 22:02
老师布置的作业,随便写写了下。
发现以前一直觉得那么难的程序,其实也就半个小时搞定了。
每个人都在进步。好好努力哈~~~
快乐的小菜鸟~~~
功能是实现从一个文本中读取单词,查询单词然后输出单词的解释~
在使用STL时候,要特别注意返回值,比如find返回迭代器,erase返回下一个迭代器,map中的count返回bool
#include<iostream>#include<string>#include<vector>#include<map>#include<fstream>/***输入单词要注意输入法的使用,我测试用QQ拼音,出现某些查询不到的情况,如果打开文件进行复制粘贴,那么就可以百分百成功又测试单词后加一个空格或者几个空格 也容易出现BUG 已知hello测试不通过 即使是复制粘贴************张海强***************20121028****************************** ****/using namespace std ;void UseVector(string & find){string str_1 ;vector<string> vec_1 ;vector<string> vec_2 ;vector<string>::iterator it_1 ;vector<string>::iterator it_2 ;cout<<"please input the location of the data:"<<endl ;cin>>str_1 ;fstream infile(str_1) ;while(!infile.eof()){string str_2 ;string str_3 ;infile>>str_2 ;infile>>str_3 ;vec_1.push_back(str_2) ;vec_2.push_back(str_3) ;}for(it_1 = vec_1.begin() ,it_2 = vec_2.begin() ; it_1 != vec_1.end() ,it_2 != vec_2.end() ;it_1 ++ ,it_2 ++ ){if(*it_1 == find){cout<<"succeed find the word!:"<<endl ;cout<<"the explaintion is:"<<endl ;cout<<*it_2 ;break ;}}if(it_1 == vec_1.end())cout<<"no such a word!!"<<endl ;}void UseMap(string & find){string str_1 ;map<string ,string> word_explian ;map<string ,string>::iterator map_1 ;cout<<"please input the location of the data:"<<endl ;cin>>str_1 ;fstream infile(str_1) ;while (!infile.eof()){string str_2 ;string str_3 ;infile>>str_2 ;infile>>str_3 ;word_explian[str_2] = str_3 ;}if(word_explian.count(find)){map_1 = word_explian.find(find) ;cout<<"succed find the word:"<<endl ;cout<<"the explaintion is:"<<endl ;cout<<map_1->second<<endl ;}}int main(){string find ;cout<<"please input the word you wanna find:"<<endl ;cin>>find ;cout<<"A:using vector to find:"<<endl ;cout<<"B:using map to find:"<<endl ;string str_2 ;cin>>str_2 ;if(str_2 == "A"){UseVector(find);}else if(str_2 == "B"){UseMap(find) ;}return 0 ;}
- STL模版库的vector,map的简单应用
- STL中map、vector的iterator的简单用法
- 数据结构应用标准模版库STL—— map的操作
- UVA 11991 STL中map、vector的应用
- STL map,vector和pair的混合应用
- stl map vector的删除
- STL标准模版库--vector的操作大全
- C++模版STL中 map 和 string, vector 的用法详解!
- c++ stl库 vector简单的实现
- c++ stl库 vector简单的实现
- stl之vector的应用
- STL------vector的简单使用
- STL vector的简单实现
- STL vector的简单用法
- STL 简单 vector 的实现
- stl的vector,map,list删除元素
- c++ STL vector map的使用
- vector的简单应用
- VSS配置及使用说明
- OpenCV2 图像加噪声
- Ubuntu12.04 在线安装JDK7
- QQ空间伤感日志:泪是我唯一的朋友
- 如何在Android 4.0 ICS中禁用StatusBar | SystemBar | 状态栏
- STL模版库的vector,map的简单应用
- 数据库中索引的优缺点
- 【搜索】靶形数独
- 二维数组的静态定义 即二维数组的输出方法--java
- 数据库索引原理(2)------MemCached
- 2012年"新秀杯"程序设计比赛——网络预选赛参考题解
- 输入两个数,求两个数的和和积
- 【枚举】工件
- 10129 - Play on Words