日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c++ bind和function

發布時間:2025/6/15 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ bind和function 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ? 先來看一段代碼:

#include<iostream> #include<functional> using namespace std;typedef std::function<void()> Functor1; typedef std::function<void(int a)> Functor2;class TEST { public:void print1(){cout << "TEST print" << endl;}void print2(int a){cout << "a=" << a << endl;} };void print3() {cout << "ptint3" << endl; }int main() {TEST test;Functor1 fc1 = std::bind(&TEST::print1,test); fc1();Functor2 fc2 = std::bind(&TEST::print2,test,placeholders::_1);fc2(5);Functor1 fc3 = std::bind(&TEST::print2,test,9);fc3();Functor1 fc4 = std::bind(print3);fc4();return 0; }

運行打印:

TEST print a=5 a=9 ptint3

? fc1是一個函數指針,由std::bind返回,參數為print1對于的地址和test對象。

? fc2中,placeholders::_1用來填補參數的占位數,fc2(2)調用時再傳參。

? fc3中,print2是帶一個int參數的,注意這里當參數不是用的占位符時,std::bind返回的是對于的不帶參數的函數指針,注意和fc2區別一下,調用時也不需要傳參。

? fc4中,print3不是屬于類的,直接傳對于的函數名即可。

?

更詳細的可以參考這個鏈接:https://www.cnblogs.com/bencai/p/9124654.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的c++ bind和function的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。