`
mmdev
  • 浏览: 12952123 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

C++ Primer 第11章 习题11.29

 
阅读更多
//11.29.cpp
//读入文本文件,存储在list容器中
//将其中重复的单词去掉,并输出输入序列中不重复的单词
#include<iostream>
#include<fstream>
#include<list>
#include<algorithm>
#include<string>
using namespace std;

//main函数接受文件名为参数
int main(int argc,char **argv)
{
	//检查命令行参数个数
	if(argc<2)
	{
		cerr<<"No input file!"<<endl;
		return EXIT_FAILURE;
	}

	//打开输入文件
	ifstream inFile;
	inFile.open(argv[1]);
	if(!inFile)
	{
		cerr<<"Can not open input file!"<<endl;
		return EXIT_FAILURE;
	}
	
	list<string> words;
	string word;

	//读入要分析的输入序列,并存放在list容器中
	while(inFile>>word)
		words.push_back(word);
	//使用list容器的操作sort对输入序列以便去除重复的单词
	words.sort();

	//使用list容器的操作unique删除输入序列中重复的单词
	words.unique();

	//输出输入序列中不重复的单词
	cout<<"unique words:"<<endl;
	for(list<string>::iterator iter=words.begin();
		iter!=words.end();++iter)
		cout<<*iter<<" ";
	cout<<endl;

	return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics