c++的一个程序问题

lty369963 2012-10-17 04:42:25
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

//用于将单词长度排序的比较函数
bool isshorter(const string &s1, const string &s2)
{
return s1.size()<s2.size();
}
//测试给定string对象的长度是否与其边界相匹配
class BET_cls{

public:
BET_cls(string::size_type len1,string::size_type len2)
{
if(len1<len2)
{
minlen=len1;
maxlen=len2;
}
else{

minlen=len2;
maxlen=len1;
}

}

bool operator()(const string &s)
{
return s.size()>=minlen && s.size()<+maxlen;
}
private:
string::size_type minlen;
string::size_type maxlen;
};
//如果ctr不为1,返回word的复制版本
string make_plural (size_t ctr,const string &word,const string &ending)
{
return (ctr==1)?word:word+ending;
}

//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;;
}
vector<string> words;
string word;
while(infile>>word)
words.push_back(word);
//对输入排序以便去除重复单词
sort(words.begin(),words.end());
//使用unique对元素重新排序并返回一个迭代器
//表示无重复单词范围结束
//erase操作删除重复的单词
words.erase(unique(words.begin(),words.end()),words.end());
//计算输出长度在1到10之间的单词数量
vector<string>::size_type wc=count_if(words.begin(),words.end(),BET_cls(1,10));
cout<<wc<<" "<<make_plural(wc,"word","s")<<endl;
return 0;
}

请问这个程序如何运行??
...全文
129 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lty369963 2012-10-18
  • 打赏
  • 举报
回复
明白了!
Linux-Torvalds 2012-10-17
  • 打赏
  • 举报
回复
命令行参数制定要输入的文件。
从文件里读取单词。
删除重复的单词。
统计长度是1~10的单词。
输出统计的结果(共有多少个)。
十八道胡同 2012-10-17
  • 打赏
  • 举报
回复
先编译出exe文件,然后

进入cmd命令行,到exe文件所在目录,敲命令
exe的名字 你的输入文件

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧