欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++11中std::function基礎用法詳解

 更新時間:2023年04月27日 10:45:39   作者:Thomas_Lbw  
std::function是C++11標準庫中提供的一種可調(diào)用對象的通用類型,它可以存儲任意可調(diào)用對象,本文就來和大家講講它的基礎用法,希望對大家有所幫助

std::function是C++11標準庫中提供的一種可調(diào)用對象的通用類型,它可以存儲任意可調(diào)用對象,如函數(shù)指針,函數(shù)對象,成員函數(shù)指針和lambda表達式。std::function類模板是一個類似于函數(shù)指針的類型,但它是可以處理任意可調(diào)用對象的,并且可以檢查調(diào)用對象是否為空。

一、std::function基本介紹

基本語法:

std::function<return_type(parameter_types)> var_name;

其中,return_type是函數(shù)返回值類型,parameter_types是函數(shù)參數(shù)類型。

舉個例子:

int func(int x, int y) { return x + y; }
std::function<int(int, int)> f = func;
class A {
public:
    int mem_func(int x) { return x * x; }
};
std::function<int(A*, int)> f2 = &A::mem_func;

std::function對象可以像普通函數(shù)一樣調(diào)用,并且可以使用bool類型的運算符來檢查調(diào)用對象是否為空。

std::function<int(int, int)> f;
if (f)
    std::cout << f(1, 2) << std::endl;
else
    std::cout << "f is empty" << std::endl;

具體使用例子:

#include <iostream>
#include <functional>
void test1(){std::cout<<"function"<<std::endl;}
int test2(int i){ return i; }
int test3(int i, int j){ return i+j; }
struct A{
    void foo(int i){ std::cout<<i<<std::endl; }
};
int main() {
    std::function<void()> fn1 = std::bind(test1);
    std::function<int(int)> fn2 = std::bind(test2, std::placeholders::_1);
    std::function<int(int, int)> fn3 = std::bind(test3, std::placeholders::_1, std::placeholders::_2);
    std::function<int(int)> fn4 = std::bind(test3, 3, std::placeholders::_1);
    std::function<int()> fn5 = std::bind(test3, 3, 4);
    A a;
    std::function<void(int)> fn6 = std::bind(&A::foo, &a, std::placeholders::_1);
    fn1();
    std::cout<<fn2(1)<<std::endl;
    std::cout<<fn3(2, 3)<<std::endl;
    std::cout<<fn4(3)<<std::endl;
    std::cout<<fn5()<<std::endl;
    fn6(8);
}

二、進階使用方法

內(nèi)容來自github,我給大家貼在下面,做個分析。

2.1 與智能指針相結合

std::function可以存儲智能指針,避免內(nèi)存泄漏:

std::function<int(int, int)> add = std::make_shared<int(*)(int, int)>([](int a, int b) { return a + b; });

這段代碼定義了一個變量add,它是一個std::function類型,這種類型可以存儲一個可調(diào)用的函數(shù)(可以是函數(shù)指針、函數(shù)對象、lambda表達式等)。該函數(shù)的簽名為int(int, int),即返回值類型為int,接受兩個int類型參數(shù)。變量add被賦值為一個指向匿名函數(shù)的指針。這個匿名函數(shù)接受兩個int類型參數(shù),并返回它們的和。使用std::make_shared<int(*)(int, int)>來創(chuàng)建該函數(shù)的共享指針。

2.2 存儲成員函數(shù)指針

調(diào)用類的成員函數(shù):

class A {
public:
    int add(int a, int b) { return a + b; }
};
std::function<int(A&, int, int)> add = &A::add;
A a;
std::cout << add(a, 3, 4) << std::endl;

這段代碼定義了一個類A,其中有一個名為add的成員函數(shù),該函數(shù)接受兩個int類型的參數(shù)并返回它們的和。然后定義了一個std::function變量add,該變量指向A類的add成員函數(shù)。接著創(chuàng)建了一個A類的對象a,最后使用std::cout輸出add(a, 3, 4)的結果。

2.3 存儲std::bind

std::function<int(int)> add3 = std::bind([](int a, int b) { return a + b; }, 3, std::placeholders::_1);
std::cout << add3(4) << std::endl;

這段代碼定義了一個std::function變量add3,該變量指向一個匿名函數(shù),該函數(shù)接受一個int類型的參數(shù)并返回它與3的和。 使用std::bind將這個匿名函數(shù)綁定到了一個函數(shù)上,并且將參數(shù)3和占位符_1綁定在這個函數(shù)上。最后使用std::cout輸出add3(4)的結果。

三、注意tips

值得注意?。?!std::function有一些限制,如不能存儲重載函數(shù)等,詳見C++標準庫文檔。

C++11標準庫文檔可以在以下網(wǎng)站中找到:

cppreference.com: std::function - cppreference.com

cplusplus.com: http://www.cplusplus.com/reference/functional/function/

C++17標準庫文檔可以在以下網(wǎng)站中找到:

cppreference.com: std::function - cppreference.com

cplusplus.com: http://www.cplusplus.com/reference/functional/function/

C++20標準庫文檔可以在以下網(wǎng)站中找到:

cppreference.com: std::function - cppreference.com

cplusplus.com: http://www.cplusplus.com/reference/functional/function/

到此這篇關于C++11中std::function基礎用法詳解的文章就介紹到這了,更多相關C++11 std::function內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C 語言進制之間的轉換

    C 語言進制之間的轉換

    本篇文章主要介紹了C語言進制之間的轉換,舉例說明并附圖片,幫助大家理解,希望對大家有所幫助
    2016-07-07
  • c++中nlohmann?json的基本使用教程

    c++中nlohmann?json的基本使用教程

    nlohmann/json 是一個C++實現(xiàn)的JSON解析器,使用非常方便直觀,下面這篇文章主要給大家介紹了關于c++中nlohmann?json基本使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • VC中LINK 2001 和 LINK 2009 的錯誤的解決方法

    VC中LINK 2001 和 LINK 2009 的錯誤的解決方法

    最近將兩個開源C++項目編譯成windows版本的時候遇到很多問題,編譯的時候總是報錯,報的最多的是無法解析的外部符號”,經(jīng)過近3天的折騰總算都通過了,這里是一些總結
    2020-10-10
  • C語言版飛機大戰(zhàn)游戲

    C語言版飛機大戰(zhàn)游戲

    這篇文章主要為大家詳細介紹了C語言版飛機大戰(zhàn)游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C++實現(xiàn)學生信息管理系統(tǒng)(Map實現(xiàn))

    C++實現(xiàn)學生信息管理系統(tǒng)(Map實現(xiàn))

    這篇文章主要為大家詳細介紹了C++實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 詳解VS2010實現(xiàn)創(chuàng)建并生成動態(tài)鏈接庫dll的方法

    詳解VS2010實現(xiàn)創(chuàng)建并生成動態(tài)鏈接庫dll的方法

    在某些應用程序場景下,需要將一些類或者方法編譯成動態(tài)鏈接庫dll,以便別的.exe或者.dll文件可以通過第三方庫的方式進行調(diào)用,下面就簡單介紹一下如何通過VS2010來創(chuàng)建動態(tài)鏈接庫
    2022-12-12
  • 淺析C++中的動態(tài)內(nèi)存分配

    淺析C++中的動態(tài)內(nèi)存分配

    這篇文章主要為大家詳細介紹了C++中動態(tài)內(nèi)存分配的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • 淺析C語言中strtol()函數(shù)與strtoul()函數(shù)的用法

    淺析C語言中strtol()函數(shù)與strtoul()函數(shù)的用法

    這篇文章主要介紹了淺析C語言中strtol()函數(shù)與strtoul()函數(shù)的用法,注意其將字符串轉換成long型的區(qū)別,需要的朋友可以參考下
    2015-08-08
  • C++設計模式編程中Template Method模板方法模式的運用

    C++設計模式編程中Template Method模板方法模式的運用

    這篇文章主要介紹了C++設計模式編程中Template Method模板方法模式的運用,講到了包括模板方法模式中的細分方法以及適用場景,需要的朋友可以參考下
    2016-03-03
  • c語言之如何求e的近似值

    c語言之如何求e的近似值

    這篇文章主要介紹了c語言之如何求e的近似值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評論