統計輸入的單詞中有幾個長度大於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*/
相關文章
- 計算2的N次冪n 可輸入,n為自然數
- 03:因子 檢視 提交 統計 提問 總時間限制: 8000ms 記憶體限制: 65536kB 描述 輸入1個整數n。從小到大依次輸出n的所有因子。 輸入 1個整數n(n<20億) 輸出 升序輸出n的所記憶體
- vue實現單點登入的N種方式Vue
- 用sql實現的n王后SQL
- HashMap的table長度為什麼是2的n次HashMap
- 幾個簡單又實用的PHP函式PHP函式
- 定義一個求n的階乘的函式函式
- 階乘之和 輸入n,計算S=1!+2!+3!+…+n!的末6位(不含前導0)。n≤10 6 ,n!表示 前n個正整數之積。
- 函式組合的 N 種模式函式模式
- 一個字串中某一個字元第n次出現的位置的函式字串字元函式
- 字串最後一個單詞的長度字串
- 不能使用for迴圈,傳入n和m, 生成一個長度為n,每一項都是m的陣列陣列
- 一個數number的n次冪 python的pow函式Python函式
- 給定一個大小為 n 的陣列,找到其中的眾數。眾數是指在陣列中出現次數大於 ⌊ n/2 ⌋ 的元素。陣列
- C/C++ Y/N 重複輸入功能實現C++
- 建立包含N個空物件的陣列物件陣列
- 分析函式——NTILE(n)函式
- 編寫程式實現 f(n)=f(n-1)+f(n-2) (f(1)=1 和 f(2)=2 )函式。函式
- c語言: 鍵盤輸入一個n個字元的串,然後按字母表順序輸出這n個字元C語言字元
- 格式化輸出n天后的時間(java實現 )Java
- 輸入3個整數a,b,c,要求按由小到大的順序將它們輸出,用函式實現函式
- C#中幾個正規表示式匹配輸入字元的函式C#字元函式
- 第十八章 34用過載輸入運算子函式實現字串的輸入函式字串
- Python單例模式(Singleton)的N種實現Python單例模式
- 類物件作為實參傳入函式,函式是複製物件還是直接使用物件的問題物件函式
- 實現自然數N的全排列
- excel統計大於15小於20的數並求和 統計區間個數的函式Excel函式
- 太原面經分享:如何用js實現返回斐波那契數列的第n個值的函式JS函式
- Excel函式應用教程:函式的輸入方法(轉)Excel函式
- 設計一個函式,傳入一個數字n,若n可以被3整除的返回1,可以被5整除返回2,可以被7整除返回3函式
- python中實現輸入指定日期,判斷輸入日期這一天是星期幾Python
- 約瑟夫問題,輸入總人數,自動產生大於0小於n的隨機數隨機
- 統計一個字串中的單詞的個數,並列印各個單詞字串
- 實現同比、環比計算的N種姿勢
- css 實現豎直居中的 N 種場景及 N 種方法CSS
- 菲波那契數——根據輸入資料中的n,輸出第n項菲波那契數
- js實現的擷取指定長度字串程式碼JS字串
- jQuery實現的擷取指定長度字串程式碼jQuery字串