日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++for_each| bind1st | ptr_fun | std::function的用法

發(fā)布時(shí)間:2023/12/13 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++for_each| bind1st | ptr_fun | std::function的用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c++ for_each 用法_小鍵233-CSDN博客

傳入?yún)?shù)

  • 要傳入?yún)?shù)給global function ,需要使用 ptr_fun() 這個(gè) function adapter 將global function 轉(zhuǎn)成function object , 然后再用bind2nd() 將參數(shù)bind成一個(gè)function object。(這句話好拗口)
void fun(int i, const char* str) {cout<<str<<i<<endl; }int main() {int a[] = { 1, 2, 3, 4};vector<int> v(a, a+sizeof(a)/sizeof(int));for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), "Element:")); } #include <iostream> #include <vector>template<class T> struct plus2{void operator()(T& x){x += 2;} };//template <class T> 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<int>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<int>()); // 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<int>arr(ia,ia+(sizeof (ia)/sizeof (int)));//移除所有小于100的元素//bind2nd(判斷條件,標(biāo)準(zhǔn)) 判斷條件 標(biāo)準(zhǔn)//bind1nd(判斷條件,標(biāo)準(zhǔn)) 標(biāo)準(zhǔn) 判斷條件/** std::bind1nd(std::less<int>(),100)) 100 < x* std::bind2nd(std::less<int>(),100)) x < 100*/ // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::less<int>(),100)),arr.end()); //100 200 // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::greater<int>(),100)),arr.end());// arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::greater<int>(),100)),arr.end()); // arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::less<int>(),100)),arr.end()); //1 2 100//刪除小于等于100的元素arr.erase(std::remove_if(arr.begin(),arr.end(),std::not1(std::bind2nd(std::greater<int>(),100))),arr.end());for(auto i : arr){std::cout << i << " ";} }

ptr_fun

  • C++11棄用 被更通用的std::function 和 std::ref替代
#include <iostream> #include <vector> #include <functional> #include <algorithm>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<char>(std::cout),std::not1(std::ptr_fun(isvowel))); }//Hll wrld!

std::function函數(shù)

  • std::function - cppreference.com?
#include <iostream> #include <vector> #include <functional> #include <algorithm>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 functionstd::function<void(int)>f_display = print_num;f_display(-9);//strore a lambdastd::function<void()> f_display_42 = [](){ print_num(43);};f_display_42();//store the result of a call to std::bindstd::function<void()>f_display_31337 = std::bind(print_num,31337);f_display_31337();//store a call to a member functionstd::function<void(const Foo&,int)>f_add_display = &Foo::print_add;const Foo foo(300000);f_add_display(foo,1);//store a call to a member function and objectusing std::placeholders::_1;std::function<void(int)>f_add_display2 = std::bind(&Foo::print_add,foo,_1);f_add_display2(2);//store a call to a member function and object ptrstd::function<void(int)>f_add_display3 = std::bind(&Foo::print_add,&foo,_1);f_add_display3(3);//store a call to a function objectstd::function<void(int)>f_display_obj = print_Num();f_display_obj(18); }

參考鏈接

  • bind1st bind2nd的使用_simahao的專欄-CSDN博客_bind2nd
  • C++ STL std::ptr_fun() 函數(shù)說明 - 簡書
  • C++中string::npos的一些用法總結(jié)_竭盡全力的專欄-CSDN博客

總結(jié)

以上是生活随笔為你收集整理的C++for_each| bind1st | ptr_fun | std::function的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。