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 ;}


 

原创粉丝点击