c++ for_each 用法_小键233-CSDN博客
传入参数- 要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global function 转成function object , 然后再用bind2nd() 将参数bind成一个function object。(这句话好拗口)
void fun(int i, const char* str)
{
cout< v(a, a+sizeof(a)/sizeof(int));
for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), "Element:"));
}
#includeptr_fun#include template struct plus2{ void operator()(T& x){ x += 2; } }; //template void plus3_fun(int& x){ x += 3; // std::cout << x << " "; } void fun(int i,const char* str){ std::cout << str << "->" << i << std::endl; } int main(){ // int ia[] = {22,30,20,34,45,64,34,56,75,34}; // std::vector iv(ia,ia+(sizeof (ia) / sizeof (int))); // for (int i : iv) { // std::cout << i << ' '; // } // std::cout << std::endl; // std::cout << iv.size() << ' '; // std::cout << std::endl; // std::for_each(iv.begin(),iv.end(),plus2 ()); // std::for_each(iv.begin(),iv.end(), std::bind2nd(std::ptr_fun(fun),"Hello world")); // for (int i : iv) { // std::cout << i << ' '; // } int ia[] = {1,2,100,200}; std::vector arr(ia,ia+(sizeof (ia)/sizeof (int))); //移除所有小于100的元素 //bind2nd(判断条件,标准) 判断条件 标准 //bind1nd(判断条件,标准) 标准 判断条件 // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::less (),100)),arr.end()); //100 200 // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::greater (),100)),arr.end()); // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::greater (),100)),arr.end()); // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::less (),100)),arr.end()); //1 2 100 //删除小于等于100的元素 arr.erase(std::remove_if(arr.begin(),arr.end(),std::not1(std::bind2nd(std::greater (),100))),arr.end()); for(auto i : arr){ std::cout << i << " "; } }
- C++11弃用 被更通用的std::function 和 std::ref替代
#includestd::function函数#include #include #include bool isvowel(char c){ return std::string("aeiouAEIOU").find(c) != std::string::npos; } int main(){ std::string s = "Hello world!"; std::copy_if(s.begin(),s.end(),std::ostreambuf_iterator (std::cout), std::not1(std::ptr_fun(isvowel))); } //Hll wrld!
- std::function - cppreference.com
#include#include #include #include struct Foo{ Foo(int num):num_(num){}; void print_add(int i)const { std::cout << num_ + i << std::endl; } int num_; }; void print_num(int i){ std::cout << i << 'n'; } struct print_Num{ void operator()(int i){ std::cout << i << 'n'; } }; int main(){ //store a free function std::function f_display = print_num; f_display(-9); //strore a lambda std::function f_display_42 = [](){ print_num(43);}; f_display_42(); //store the result of a call to std::bind std::function f_display_31337 = std::bind(print_num,31337); f_display_31337(); //store a call to a member function std::function f_add_display = &Foo::print_add; const Foo foo(300000); f_add_display(foo,1); //store a call to a member function and object using std::placeholders::_1; std::function f_add_display2 = std::bind(&Foo::print_add,foo,_1); f_add_display2(2); //store a call to a member function and object ptr std::function f_add_display3 = std::bind(&Foo::print_add,&foo,_1); f_add_display3(3); //store a call to a function object std::function f_display_obj = print_Num(); f_display_obj(18); }
参考链接
- bind1st bind2nd的使用_simahao的专栏-CSDN博客_bind2nd
- C++ STL std::ptr_fun() 函数说明 - 简书
- C++中string::npos的一些用法总结_竭尽全力的专栏-CSDN博客



