问题
重写统计长度小于等于6的单词数量的程序,使用bind函数代替lambda.
ps:昨晚因为导入库的问题,搞了一晚上都没搞好,所以导入一定要谨慎!
源代码
注意:一定要引入
1 | using namespace std::placeholders; |
,否则报错!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /************************************************************************* > File Name: test.10.22.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年06月16日 ************************************************************************/ #include<iostream> #include<functional> #include<algorithm> #include<vector> using namespace std; using namespace std::placeholders; /********************************** 重写统计长度小于等于6的单词数量的程序,使用函数代替lambda. **************************************/ bool isshorter(const string &a,string::size_type sz) { return !((a.size())<=sz); } ostream &out(ostream &os,const string &s) { return os << s << "\n "; } void elis(vector<string> &vec,string::size_type sz) { sort(vec.begin(),vec.end()); cout << "**************************\n排序后\n**********************" << endl; for_each(vec.begin(),vec.end(),bind(out,ref(cout), _1)); //auto fre = unique(vec.begin(),vec.end(),bind(isshorter, _1, sz)); auto fre = unique(vec.begin(),vec.end()); cout << "************************\n排重后\n***********************" << endl; for_each(vec.begin(),vec.end(),bind(out,ref(cout), _1)); vec.erase(fre,vec.end()); cout << "************************\n去重后\n**********************" << endl; for_each(vec.begin(),vec.end(),bind(out,ref(cout), _1)); cout << "**********************\n\n计算结果\n\n******************" << endl; auto findifs = partition(vec.begin(),vec.end(),bind(isshorter , _1 , sz)); auto count = vec.end()-findifs; cout << "有:" << count << "个元素小于等于:" << 6 << endl; } int main() { vector<string> vec; string pa; cout << "你可以输入数字,字母[Linux下面按ctrl+D结束输入]:" << endl; while(cin >> pa) { vec.push_back(pa); } elis(vec,6); return 0; } |