C++ 统计文本中所有字符串出现次数

#include 
#include 
#include 
#include
#include 

#include 
#include 

using namespace std;

int main()
{
	fstream infile;

	mapMaps;
	infile.open("words.txt");   //将文件流对象与文件连接起来 

	if (!infile.is_open())
	{
		cout << "open file error" << endl;
		return 0;
	}//若失败,则输出错误消息,并终止程序运行 

	string strfile, tmp;

	while (getline(infile, tmp))
	{
		strfile.append(tmp);
		strfile.append(" ");//一定要加这一行!!!因为getline是直接读取文本文件的一行,遇    //到回车换行符作为读取一行的结束,如果有如下情况:
                                     //abc bcf
                                     //a  如果读完一行不加空格那么读取完abc bcf后再读取a,//则bcfa为一个字符串,算作一个了
		tmp.clear();
	}

	for (int i = 0; i> tmp)
	{
		if (Maps.find(tmp) == Maps.end())
		{
			Maps.insert(pair(tmp, 1));
		}
		else
		{
			Maps[tmp]++;
		}
	}

	for (map::iterator it = Maps.begin(); it != Maps.end(); it++)
	{
		cout << it->first << it->second << endl;
	}

	infile.close();             //关闭文件输入流  

	system("pause");
	return 0;
}

抄的别人的:https://blog.csdn.net/FX677588/article/details/72643302

你可能感兴趣的:(C++ 统计文本中所有字符串出现次数)