统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现
2014-07-23 14:28
471 查看
#ifndef COUNT_WORD_H #define COUNT_WORD_H #include <string.h> #include <iostream> #include <iterator> #include <vector> #include <algorithm> class GT_cls{ public: GT_cls(size_t val = 0) :bound_(val){} bool operator()(const std::string &s){ return s.size() >= bound_; } private: std::string::size_type bound_; }; class Word_count{ public: Word_count(size_t val) :GT_(val), wc_(0){} void readWord(); void process(); void display(); private: static bool isShorter(const std::string &s1, const std::string &s2); std::vector<std::string> words_; GT_cls GT_; size_t wc_; }; inline void Word_count::readWord(){ std::istream_iterator<std::string> cin_it(std::cin); std::istream_iterator<std::string> end_of_stream; while(cin_it != end_of_stream){ words_.push_back(*cin_it++); } } inline void Word_count::process(){ sort(words_.begin(), words_.end()); std::vector<std::string>::iterator end_unique = \ unique(words_.begin(), words_.end()); words_.erase(end_unique, words_.end()); stable_sort(words_.begin(), words_.end(), isShorter); wc_ = count_if(words_.begin(), words_.end(), GT_); } inline void Word_count::display(){ std::cout << "There are " << wc_ << " words." << std::endl; for(std::vector<std::string>::iterator it = words_.begin(); it != words_.end(); ++it){ std::cout << *it << " "; } std::cout << std::endl; } inline bool Word_count::isShorter(const std::string &s1, const std::string &s2){ return s1.size() < s2.size(); } #endif /*COUNT_WORD_H*/
相关文章推荐
- 打开文件读取信息,统计单词长度大于指定值的个数
- //1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, //输入9,输出9 * 9口诀表,输出12,输出12 * 12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入多少打印几乘几的乘法表
- 1. 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9 * 9口诀表,输出12,输出12 * 12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入多少打印几乘几的乘法表
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。 (无返回值需要传参)
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 编写函数,对标准输入进行扫描,并对“the”出现的次数进行计数。 进行比较时应区分大小写。认为一个单词有一个或多个空格字符分隔, 并且输入行在长度上不会超过100个字符,计数结果输出。
- [原创]c++,自己写的一个统计输入的单词个数,行数和字符总数的小程序
- js 取一个对象的长度,取出来的是undefined,自己写的一个计算长度的函数解决了。
- 编写一个程序,定义一个职工类,输入3个职工的编号、姓名、工资和年龄, 类中的成员函数实现输入、输出,在主函数中定义对象,并计算输出3个职工的平均工资。
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊;这里做个小结,为了自己复习,也希望对后来者能有所
- 假如现在有一堆长度大于3小于9的电话号码,用座机呼叫,如果出现这样的号码【123和12345】那么12345将永远不会被拨出,因为拨到123的时候电话已经呼出了,试写一个函数输出所有不能被呼出的电话号码(java实现)
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- setInterval函数简单实现实时统计字符长度