一个很精彩的关于STL的例程

下面是C++ primer上关于STL的一个完整的历程,程序的目的在于统计需要查找的一个英文单词在一个英文文本文件中出现多少次,以及输有要查找单词出现的那一行。

下面的程序可以帮助你使用STL时有一个更便捷的参考,在彻底弄清楚下面的程序后你完全可以改写一下,统计出一个英文文本中每个单词出现的次数。这样你就可以根据这个结果查找那些不会的单词,重点查一下词典,这样应该能更好的去阅读一篇英文文章。


#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <string>


using namespace std;

void make_map(ifstream&,vector<string>&,map<string,map<vector<string>::size_type,int> >&);
ifstream& open_file(ifstream&,string&);
void make_vector(ifstream&,vector<string>&);

//新建文件输入流并与文件关联
ifstream& open_file(ifstream& inFile,string& filename)
{
	cout << "请输入文件名:" << endl;
	cin >> filename;

	inFile.close();
	inFile.clear();     //重置输入流的状态位
	inFile.open(filename.c_str());
	return inFile;
}

//建立vector<string>类型的对象,元素为文本的一行字符串
void make_vector(ifstream& inFile,vector<string>& file_line)
{
	string strline;       //文本中取出一行放入strline中

	//vector<string>类型的对象引用file_line中的每个元素存放文本的一行内容,文本由多少行,file_line中就有多少元素
	while(getline(inFile,strline))
	{
		file_line.push_back(strline);
	}
}

//建立map<string,map<vector<string>::size_type,int> >用于存储在文本中出现的单词(string)
//出现的行号(vector<string>::size_type),以及此行出现的次数(int)
void make_map(ifstream& inFile,vector<string>& file_line,map<string,map<vector<string>::size_type,int> >& wordInfo)
{
    vector<string>::size_type linecount = file_line.size();  //获取文本的行数信息

    //以下代码将要建立map<string,map<int,int> >对象的引用wordInfo,用于存储文本中单词
    //的信息,包括出现的次数,在哪行出现过,以及此行中出现的次数
    for(vector<string>::size_type ix=0; ix!=linecount; ++ix)
    {
    	istringstream inword(file_line[ix]); //建立字符串的输入流,用于对一行文本进行解析单词
    	if(!inword)
    		cerr << "文本行载入出错!" << endl;
    	string word; 
    	while(inword>>word)  //从字符串输入流中逐个取出单词
    	{
    		//如果文本中还没有出现过这个单词,在map中直接插入此单词,以及当前行数,当前在此行出现的次数1
    		if(!wordInfo.count(word))
    		{
    			map<vector<string>::size_type,int> wordIndex;
    			wordIndex.insert(make_pair(ix,1));
    			wordInfo.insert(make_pair(word,wordIndex));
    		}
    		//次word单词已经在文本中出现过
    		else
    		{
    			//但是word单词没有在此行出现过
    			if(!wordInfo.find(word)->second.count(ix))
    			{
    				pair<vector<string>::size_type,int> curLine = make_pair(ix,1);
    				wordInfo.find(word)->second.insert(curLine);
    			}
    			else  //如果当前行出现过此单词
    			{
    				++(wordInfo.find(word)->second.find(ix)->second);
    			}
    		}
    	}
    }
}

int main(int argc,char *argv[])
{
	string word;               //将要查找的单词
	string filename;           //将要查找的文件名
	ifstream inFile;           //关联文件的输入流

    //调用open_file,读入文件,如果打开文件失败,重新输入文件名
	while(!open_file(inFile,filename))
	{
		cerr << "文件载入错误!";
	}

	vector<string> file_line;   //每个元素存储文本的一行字符串
	make_vector(inFile,file_line);   //调用make_vector,建立vector对象

	map<string,map<vector<string>::size_type,int> > wordInfo;  //用于存储单词的信息
	make_map(inFile,file_line,wordInfo);     //调用make_map建立单词信息
	cout << "请输入要查询的单词";
	while(cin >> word)
	{
		int count = 0;
		map<string,map<vector<string>::size_type,int> >::iterator it = wordInfo.find(word);
		if(it == wordInfo.end())
		{
			cout << "此单词不存在!";
			cout << "请输入要查询的单词";
			continue;
		}

		map<vector<string>::size_type,int>::iterator beg = it->second.begin(),
		                                             end = it->second.end();

		while(beg!=end)
		{
			count = count+beg->second;
			++beg;
		}
		cout << word << "共出现" << count << "次!" << endl;

		beg = it->second.begin();

		while(beg != end)
		{
			cout << "行数" << beg->first+1 << ":" << file_line[beg->first] << endl;
			++beg;
		}
		cout << "请输入要查询的单词:";
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值