统计输入的单词中有几个长度大于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是自己指定的,用函数对象实现

时间: 2024-08-01 13:41:15

统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现的相关文章

C 循环统计输入的单词个数和字符长度

#include <stdio.h> #include <Windows.h> int main(void) { char word[128]; int count = 0; int length = 0; printf("请输入任意多个单词:\n"); while (1) { if (scanf("%s",word) != -1) { count++; length += strlen(word); } else { break; } }

按长度统计输入单词的出现频率,并以直方图的形式打印。

1-13 #include <stdio.h> #define OUT 0#define IN 1#define MAXHIST 15 //最大直方图#define MAXWORD 11 //最大单词长度 int main(){ int c, i ,nc, state; //当前字符, , ,状态 int len; //每个直方图的长 int maxvalue; //wl[]的最大值(单词最高频率) int ovflow;//超出长度(MAXWORD)的单词数 int wl[MAXWORD];

算法竞赛入门经典 习题3-1 分数统计 习题 3-2 单词的长度

习题3-1 分数统计 输入一些学生的分数,哪个分数出现的次数最多?如果有多个并列,从小到大输出. 任务1:分数均不超过100的非负整数 任务2:分数均不超过100的非负实数,但最多保留两位小数. 任务1 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 101 + 10 int a[MAXN]; int main(int argc, char *argv[]) { int n,

统计输入的行数,单词数和字符数

#include<stdio.h> #define IN 1 /*在单词内*/ #define OUT 0 /*在单词外*/ //统计输入的行数,单词数和字符数 int main(){ int c, nl, nw, nc, state; nw=nl=nc=0; state=OUT; while((c=getchar())!=EOF){ nc++; if(c=='\n') nl++; if(c=='\n' || c=='\t' || c==' '){ state=OUT; } else if(s

输入一行字符,统计其中有多少单词,单词之间用空格隔开

问题描述: 输入一行字符,统计其中有多少单词,单词之间用空格隔开 解题思路: 判断单词是否出现,可以用空格的出现来判断(连续的若干空格看做成一个),若当前字符为空格,表明word未出现,当前字符非空格,之前字符为空格表明新单词出现,count++,之前字符是否为空格,用状态标志位word来标记 代码如下: #include<stdio.h> //printf #include<string.h> //gets #include<stdlib.h> //system #d

实时统计输入字符个数

JavaScript中经常要实时统计输入的个数,用onkeyup只对输入英文有效果,输入中文时没办法实时统计:而使用onchange,也只能是等输入框失去焦点才能统计,并不能实时.下面的方法就可以实现实时统计: <div id="msg"></div> <input id='txt' value="" /> <script> //当状态改变的时候执行的函数 function handle() { document.ge

简单的方法来统计文件中单词和各种标点符号个数

此小程序使用最基本的方法来统计文本中英文单词的个数,想法也比较简单: (1)从文本中文本读取内容,使用BufferedReader类每次读取一行并添加到StringBuffer类型变量中, 最后StringBuffer类型变量即为文本的内容,如StringBuffer sb: (2)把sb的内容全部转化成小写字母(或大写字母): (3)统计文件中各种标点符号个数: (4)把所有标点符号统一替换成一种标点符号,如替换成逗号 (5)替换后的文本使用字符串的分割函数来获取返回的字符串数组的长度,此长度

Python(30)_统计输入的字符串有多少数字

#-*-coding:utf-8-*- ''' 统计用户输入的字符串中有几个数字 ''' # numList = ['0','1','2','3','4','5','6','7','8','9'] s = input('请输入字符串:') count = 0 for i in s: if i in numList: count +=1 print(count) Python(30)_统计输入的字符串有多少数字 原文地址:https://www.cnblogs.com/sunnybowen/p/1

编写一个程序,统计输入字符串中每一个小写英文字母出现的次数

import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/1 22:18 * @description: * @version:$ */ /*编写一个程序,统计输入字符串中每一个小写英文字母出现的次数*/ public class page0901 { public static void main(String[] args) { /*首先,输入一段字符串作为字符数组*/ System.out.p