+ -
当前位置:首页 → 问答吧 → 求助

求助

时间:2011-12-04

来源:互联网

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

///
bool isShorter(const string &s1,const string &s2)
{
  return s1.size() < s2.size();
}
////
bool GT4(const string &s)
{
  return s.size() >= 4;
}
////
string make_plural(size_t ctr,const string &word,const string &ending)
{
  return (ctr == 1) ? word : word + ending;
}

////////////////////////////
int main(int argc,char **argv)
{
  if(argc < 2)
{
  cerr << "NO FILE INPUT!" <<endl;
  return EXIT_FAILURE;  
}
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr << "can't open the file!" <<endl;
return EXIT_FAILURE;
}

vector<string> words;
string word;
while (inFile >> word)

words.push_back(word);

sort(words.begin(),words.end());

words.erase(unique(words.begin(),words.end()),words.end());
   
stable_sort(words.begin(),words.end(),isShorter);

vector<string>::size_type wc = count_if (words.begin(),words.end(),GT4);
cout << wc << "" << make_plural(wc,"word","s") << "4 characters or longer" <<endl;
cout << "unique words: " <<endl;
for (vector<string>::iterator iter = words.begin();iter !=words.end();++iter)
cout << *iter << " ";
cout << endl;
return 0;
}
程序功能统计长度不小于4的单词,并输出输入序列中不重复的单词。
怎么把文件输入呢?编译运行出现:argc = 1,"NO FILE INPUT!" 
哪位同仁指导一下,谢谢~

作者: yang_xiao_tao   发布时间: 2011-12-04

命令行参数输入你的文件名
 

作者: sduxiaoxiang   发布时间: 2011-12-04