亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++11中std::function基礎(chǔ)用法詳解

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

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

一、std::function基本介紹

基本語法:

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

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

舉個(gè)例子:

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對(duì)象可以像普通函數(shù)一樣調(diào)用,并且可以使用bool類型的運(yùn)算符來檢查調(diào)用對(duì)象是否為空。

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);
}

二、進(jìn)階使用方法

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

2.1 與智能指針相結(jié)合

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

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

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

2.2 存儲(chǔ)成員函數(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;

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

2.3 存儲(chǔ)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;

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

三、注意tips

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

C++11標(biāo)準(zhǔn)庫文檔可以在以下網(wǎng)站中找到:

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

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

C++17標(biāo)準(zhǔn)庫文檔可以在以下網(wǎng)站中找到:

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

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

C++20標(biāo)準(zhǔn)庫文檔可以在以下網(wǎng)站中找到:

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

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

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

相關(guān)文章

  • C 語言進(jìn)制之間的轉(zhuǎn)換

    C 語言進(jìn)制之間的轉(zhuǎn)換

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

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

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

    VC中LINK 2001 和 LINK 2009 的錯(cuò)誤的解決方法

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

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

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

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

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

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

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

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

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

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

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

    C++設(shè)計(jì)模式編程中Template Method模板方法模式的運(yùn)用

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

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

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

最新評(píng)論