问题
- bind接受几个参数?
- 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值.
源码
必须引入
1 | using namespace std::placeholders; |
!!!
1:
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 | /************************************************************************* > File Name: test.10.23.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年06月18日 ************************************************************************/ #include<iostream> #include<functional> using namespace std; using namespace std::placeholders; /*************************************** bind接受几个参数. ********************************************/ int add(int a,int b,int c,int d,int e,int f,int g) { return a+b+c+d+e+f+g; } int main() { cout << "bind接受几个参数?\n答:bind(method,_n),n为整数个." << endl; auto f = bind(add, _1 , _2 , _3 , _4 , _5 , _6 , _7); cout << f(1,2,3,4,5,6,7) << endl; return 0; } |
2:
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 | /************************************************************************* > File Name: test.10.24.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年06月17日 ************************************************************************/ /******************************* 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值. ***************************/ #include<iostream> #include<algorithm> #include<vector> using namespace std; using namespace std::placeholders; bool check_size(const string &s,int a) { return a>s.size(); } ostream &out(ostream &os,const int &a) { return os << a << "\n" ; } void elis(vector<int> vec,string &rs) { sort(vec.begin(),vec.end()); auto re = unique(vec.begin(),vec.end()); vec.erase(re,vec.end()); auto finds = find_if(vec.begin(),vec.end(),bind(check_size, rs , _1 )); auto counts = vec.end()-finds; cout << "已经处理过[去重,排序]:" << endl; for_each(vec.begin(),vec.end(),bind(out,ref(cout), _1 )); cout << "****************************" << endl; cout << "一共有:" << counts << "个元素大于:" << rs << "的长度" << endl; } int main() { vector<int> vec; int pa ; cout << "输入一组数字:" << endl; while(cin >> pa) { vec.push_back(pa); } string rs = "hello"; elis(vec,rs); return 0; } |