问题
- bind接受几个参数?
- 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值.
源码
必须引入using namespace std::placeholders;
!!!
1:
/*************************************************************************
> File Name: test.10.23.cpp
> Author: puruidong
> Mail: 1@w1520.com
> Created Time: 2014年06月18日
************************************************************************/
#include
#include
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:
/*************************************************************************
> 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
#include
#include
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
{
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 pa ;
cout << "输入一组数字:" << endl;
while(cin >> pa)
{
vec.push_back(pa);
}
string rs = "hello";
elis(vec,rs);
return 0;
}