统计输入的单词中有几个长度大于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编程,但是遇到一个问题,解决不了. 问题如下: ...
随机推荐
- Android-Binder 简析
前言 对于Android来说,Binder的重要性怎么说都不为过.不管是我们的四大组件Activity.Service.BroadcastReceiver.ContentProvider,还是经常在应 ...
- mysql的三大范式
关系数据库的几种设计范式介绍: 第一范式:确保每列的原子性(强调的是列的原子性,即列不能够再分成其他几列). 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式 ...
- 【TCP/IP】IP路由选择
IP层在内存中有一个路由表,当有数据要发送时.它要对该表进行一次搜索以确认转发地址.收到的数据到达IP层时,IP层会检查数据报的目的地址是否为本机IP或广播IP: 假设是.就依据IP首部协议字段的协议 ...
- python scrapy简单爬虫记录(实现简单爬取知乎)
之前写了个scrapy的学习记录,只是简单的介绍了下scrapy的一些内容,并没有实际的例子,现在开始记录例子 使用的环境是python2.7, scrapy1.2.0 首先创建项目 在要建立项目的目 ...
- appium python api(转)
Appium_Python_Api文档 1.contextscontexts(self): Returns the contexts within the current session. 返回当前会 ...
- 自定义ViewPager的兼容性问题及解决办法
通过它我们可以给图片增加组合动画效果,也可以写成一个图片查看器. 比如我们首次安装应用的时候,很多就会用到ViewPager给我们做一个应用简介.今天要写的也是这个--怎么用ViewPager实现动画 ...
- Dynamics CRM 2015/2016 Web API:Unbound Action 和 Bound Action
上篇文章介绍了Bound/Unbound Function.今天我们来看看Action吧.像我之前说的:Function和Action之前的差别能够简单理解为.Function不改动数据,可是Acti ...
- 使用php在服务器端生成图文验证码
图文验证码的实现原理: 1):准备些许图片将其存储在数据库,每一张图片对应一个标识字段. 2):在服务器端使用数组的形式将图片与标识字段组合起来. 3):随机给客户端返回图片,并接受用户输入的字段. ...
- 介绍C#结构体与类区别
1. 结构体与类定义方式 结构体定义使用struct类定义使用class 结构体: struct testDemo{ int num; void action(){ } } 类: class test ...
- Atitit.5gl 第五代语言编程语言 PROLOG教程 人工智能语言的标准 与实现
Atitit.5gl 第五代语言编程语言 PROLOG教程 人工智能语言的标准 与实现 1. 第五代语言就是自然语言又被称为知识库语言或人工智能语言,1 2. 人工智能语言特点2 2.1. 试探法2 ...