首页 > 其他 > 详细

lambda与函数调用的转换

时间:2014-08-26 00:22:45      阅读:291      评论:0      收藏:0      [点我收藏+]

14.38 编写一个类令其检查某个给定的string对象的长度是否与一个阈值相等。使用该对象编写程序,统计并报告在输入的文件中长度为1的单词有多少个,长度为2的单词有多少个、.....、长度为10的单词又有多少个。

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<fstream>
using namespace std;

class Length
{
public:
    Length(size_t n):sz(n) {}
    bool operator()(string &s){ return s.size()==sz;}
private:
    size_t sz;
};

int main()
{
    ifstream in("4.txt");
    string str;
    vector<string> vec;
    int arr[10];
    while(in>>str)
        vec.push_back(str);
    for(size_t i=0;i<10;++i)
        arr[i]=count_if(vec.begin(),vec.end(),Length(i+1));
    for(size_t i=0;i<10;++i)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

 

lambda与函数调用的转换

原文:http://www.cnblogs.com/wuchanming/p/3936152.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!