统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现
#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*/
统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现的更多相关文章
- C 循环统计输入的单词个数和字符长度
C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- C 统计用户输入的总行数和字符长度
C 统计用户输入的总行数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; printf ...
- asp.net——统计输入的字符数目
asp.net——统计输入的字符数目 题目: 在页面中有一个TextBox输入框,一个显示文字用的Label,一个提交按钮Button.在TextBox中输入一段英文字母,点击按钮提交后统计其中字母‘ ...
- 已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description ...
- 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度
import java.util.Scanner; /** * [程序38] * * 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. * * @author Jame ...
- C# 使用js正则表达式,让文本框只能输入数字和字母,最大长度5位
使用js正则表达式,让文本框只能输入数字和字母,最大长度5位,只需要加个onkeyup事件,即可简单实现 <asp:TextBox ID="txtBegin" runat=& ...
- PHP统计字符串里单词查询关键字
<?function full_count_words($str) { //返回完整数组,包含字符串里每个单词 $words = str_word_count($str,1); ...
- 输入n个数组,数组长度不等,每个数组取出一个数进行组合,求出所有的组合。
转载声明:原文转自http://www.cnblogs.com/xiezie/p/5511707.html 昨天晚上,有个朋友找到我,他在用matlab编程,但是遇到一个问题,解决不了. 问题如下: ...
随机推荐
- XCode下的iOS单元测试
XCode 内置了 OCUnit 单元测试框架,但目前最好用的测试框架应该是 GHUnit.通过 GHUnit + OCMock 组合,我们可以在 iOS 下进行较强大的单元测试功能.本文将演示如何在 ...
- kubernetes API Server安全
用户访问API Server(以下简称Server),K8S的安全检查步骤:认证和授权. 认证解决用户是谁的问题,就是验证用户名密码;授权解决用户能做什么的问题,就是检查该用户是否拥有权限访问请求的资 ...
- 十步完全理解 SQL
很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语言.甚至是函数语言(尽管有些人认为 SQL 语言也是一种函数式语言) ...
- tensorflow seq2seq.py接口实例
以简单英文问答问题为例测试tensorflow1.4 tf.contrib.legacy_seq2seq中seq2seq文件的几个seq2seq接口 github:https://github.com ...
- 一个例子理解Predicate、Consumer和Stream
一个需求: 把年龄大于20的学生的信息打印出来. 面向对象编程 public class Student { private String name; private int age; private ...
- 2017.7.1 mysql安装与启动(已验证可以使用)
下载地址:http://learning.happymmall.com/ 之前一直用解压版安装,启动mysql服务的时候总是失败,这次用mysql installer安装一遍,终于成功启动. 1.下载 ...
- 怎么将JSP页面的ID值传给Action进行更新和删除
这里只是单纯的SH整合. JSP页面代码 <!-- value=action中数据库的User对象集合list必须和action定义的名字一样, 且为了在这里能够访问,需要生成get/set方法 ...
- Oracle 时间 MM-dd形式转换
SELECT TO_CHAR( SYSDATE,'MM-dd') AS beginTime,TO_CHAR( TO_DATE(MAX(C.SUBSCRIBE_DATE),'YYYY-MM-dd'),' ...
- mvc Ajax 跨域请求
js端: $.ajax({ type : "get", async : false, url :url, data: 'bid=0&xingming=' + uName + ...
- string转object-兼容低版本浏览器(eval实现)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...