亚洲精品久久久久久久久久久,亚洲国产精品一区二区制服,亚洲精品午夜精品,国产成人精品综合在线观看,最近2019中文字幕一页二页

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

std::function簡介及模板類聲明

5jek_harmonyos ? 來源:編程學(xué)習(xí)總站 ? 作者:寫代碼的牛頓 ? 2021-07-28 15:30 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

01

std::function簡介

std::function是一個函數(shù)包裝器,該函數(shù)包裝器模板能包裝任何類型的可調(diào)用實體,如普通函數(shù),函數(shù)對象,lamda表達(dá)式等。包裝器可拷貝,移動等,并且包裝器類型僅僅依賴于調(diào)用特征,而不依賴于可調(diào)用元素自身的類型。

std::function是C++11的新特性,包含在頭文件《functional》中。一個std::function類型對象實例可以包裝下列這幾種可調(diào)用實體:函數(shù)、函數(shù)指針、成員函數(shù)、靜態(tài)函數(shù)、lamda表達(dá)式和函數(shù)對象。

std::function對象實例可被拷貝和移動,并且可以使用指定的調(diào)用特征來直接調(diào)用目標(biāo)元素。當(dāng)std::function對象實例未包含任何實際可調(diào)用實體時,調(diào)用該std::function對象實例將拋出std::bad_function_call異常。02

std::function實戰(zhàn)

std::function模板類聲明

template《class _Rp, class 。。._ArgTypes》 class _LIBCPP_TEMPLATE_VIS function《_Rp(_ArgTypes.。。)》 : public __function::__maybe_derive_from_unary_function《_Rp(_ArgTypes.。。)》, public __function::__maybe_derive_from_binary_function《_Rp(_ArgTypes.。。)》 { 。。. }std::function模板類成員函數(shù)聲明

typedef _Rp result_type; // construct/copy/destroy: _LIBCPP_INLINE_VISIBILITY function() _NOEXCEPT { } _LIBCPP_INLINE_VISIBILITY function(nullptr_t) _NOEXCEPT {} function(const function&); function(function&&) _NOEXCEPT; template《class _Fp, class = _EnableIfCallable《_Fp》》 function(_Fp); #if _LIBCPP_STD_VER 《= 14 template《class _Alloc》 _LIBCPP_INLINE_VISIBILITY function(allocator_arg_t, const _Alloc&) _NOEXCEPT {} template《class _Alloc》 _LIBCPP_INLINE_VISIBILITY function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {} template《class _Alloc》 function(allocator_arg_t, const _Alloc&, const function&); template《class _Alloc》 function(allocator_arg_t, const _Alloc&, function&&); template《class _Fp, class _Alloc, class = _EnableIfCallable《_Fp》》 function(allocator_arg_t, const _Alloc& __a, _Fp __f); #endif function& operator=(const function&); function& operator=(function&&) _NOEXCEPT; function& operator=(nullptr_t) _NOEXCEPT; template《class _Fp, class = _EnableIfCallable《_Fp》》 function& operator=(_Fp&&); ~function(); // function modifiers: void swap(function&) _NOEXCEPT; #if _LIBCPP_STD_VER 《= 14 template《class _Fp, class _Alloc》 _LIBCPP_INLINE_VISIBILITY void assign(_Fp&& __f, const _Alloc& __a) {function(allocator_arg, __a, _VSTD::forward《_Fp》(__f)).swap(*this);} #endif // function capacity: _LIBCPP_INLINE_VISIBILITY _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return static_cast《bool》(__f_); } // deleted overloads close possible hole in the type system template《class _R2, class.。。 _ArgTypes2》 bool operator==(const function《_R2(_ArgTypes2.。。)》&) const = delete; template《class _R2, class.。。 _ArgTypes2》 bool operator!=(const function《_R2(_ArgTypes2.。。)》&) const = delete; public: // function invocation: _Rp operator()(_ArgTypes.。。) const; #ifndef _LIBCPP_NO_RTTI // function target access: const std::type_info& target_type() const _NOEXCEPT; template 《typename _Tp》 _Tp* target() _NOEXCEPT; template 《typename _Tp》 const _Tp* target() const _NOEXCEPT; #endif // _LIBCPP_NO_RTTI從成員函數(shù)里我們知道std::function對象實例不允許進(jìn)行==和!=比較操作,std::function模板類實例最終調(diào)用成員函數(shù)_Rp operator()(_ArgTypes.。。) const進(jìn)而調(diào)用包裝的調(diào)用實體。1、std::function包裝函數(shù)指針定義一個std::function《int(int)》對象實例

std::function《int(int)》 callback;std::function對象實例包裝函數(shù)指針

int (*fun_ptr)(int); int fun1(int a){ return a; } int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; fun_ptr = fun1; //函數(shù)指針fun_ptr指向fun1函數(shù) callback = fun_ptr; //std::function對象包裝函數(shù)指針 std::cout 《《 callback(10) 《《 std::endl; //std::function對象實例調(diào)用包裝的實體 return 0; }2、std::function包裝函數(shù)

int fun1(int a){ return a; } int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = fun1; //std::function包裝函數(shù) std::cout 《《 callback(42) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }3、std::function包裝模板函數(shù)

template《typename T》 T fun2(T a){ return a + 2; } int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = fun2《int》; //std::function包裝模板函數(shù) std::cout 《《 callback(10) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }4、std::function包裝函數(shù)對象

struct add{ int operator()(int x){ return x + 9; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = add(); //std::function包裝對象函數(shù) std::cout 《《 callback(2) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }5、std::function包裝lamda表達(dá)式

int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; auto fun3 = [](int a) {return a * 2;}; //lamda表達(dá)式 callback = fun3; //std::function包裝lamda表達(dá)式 std::cout 《《 callback(9) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

6、std::function包裝模板對象函數(shù)

template 《typename T》 struct sub{ T operator()(T a){ return a - 8; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = sub《int》(); //std::function包裝模板對象函數(shù) std::cout 《《 callback(2) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

7、std::function包裝模板對象靜態(tài)函數(shù)

template 《typename T》 struct foo2{ static T foo(T a){ return a * 4; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = foo2《int》::foo; //std::function包裝模板對象靜態(tài)函數(shù) std::cout 《《 callback(3) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

8、std::function包裝對象靜態(tài)函數(shù)

struct foo1{ static int foo(int a){ return a * 3; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = foo1::foo; //std::function包裝對象靜態(tài)函數(shù) std::cout 《《 callback(5) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

9、std::function包裝類成員函數(shù)

struct foo3{ int foo(int a){ return a * a; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; foo3 test_foo1; callback = std::bind(&foo3::foo, test_foo1, std::_1); //std::function包裝類成員函數(shù) std::cout 《《 callback(9) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

這里我們用到了std::bind,C++11中std::bind函數(shù)的意義就如字面上的意思一樣,用來綁定函數(shù)調(diào)用的某些參數(shù)。std::bind的思想其實是一種延遲計算的思想,將可調(diào)用對象保存起來,然后在需要的時候再調(diào)用。而且這種綁定是非常靈活的,不論是普通函數(shù)還是函數(shù)對象還是成員函數(shù)都可以綁定,而且其參數(shù)可以支持占位符。

這里的std::_1是一個占位符,且綁定第一個參數(shù),若可調(diào)用實體有2個形參,那么綁定第二個參數(shù)的占位符是std::_2。

10、std::function包裝模板類成員函數(shù)

template 《typename T》 struct foo4{ T foo(T a){ return a * 6; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; foo4《int》 test_foo2; callback = std::bind(&foo4《int》::foo, test_foo2, std::_1); //std::function包裝模板類成員函數(shù) std::cout 《《 callback(7) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

11、std::function拷貝、移動

int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; std::function《int(int)》 callback2 = callback; //拷貝賦值運(yùn)算符 std::cout 《《 callback2(7) 《《 std::endl; std::function《int(int)》&& callback3 = std::move(callback); //移動賦值運(yùn)算符 std::cout 《《 callback3(7) 《《 std::endl; std::cout 《《 callback(7) 《《 std::endl; std::function《int(int)》 callback4(callback); //拷貝 std::cout 《《 callback4(7) 《《 std::endl; return 0; }

編輯:jq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • STD
    STD
    +關(guān)注

    關(guān)注

    0

    文章

    36

    瀏覽量

    14637

原文標(biāo)題:C++ std::function詳解與實戰(zhàn)

文章出處:【微信號:harmonyos_developer,微信公眾號:harmonyos_developer】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    STD80N240K6功率MOSFET技術(shù)解析與應(yīng)用指南

    開關(guān)速度和低損耗。集成ESD保護(hù)二極管提高了STD80N240K6 MOSFET的整體耐用性,高達(dá)2人體模型(HBM)。與上一代的MDmesh K5相比,MDmesh K6 MOSFET具有更低
    的頭像 發(fā)表于 10-30 11:39 ?166次閱讀
    <b class='flag-5'>STD</b>80N240K6功率MOSFET技術(shù)解析與應(yīng)用指南

    目標(biāo)追蹤的簡易實現(xiàn):模板匹配

    重新出現(xiàn)在幀圖象中時,迅速地重新捕捉到目標(biāo)。 以上對目標(biāo)追蹤這一領(lǐng)域做了簡要的說明,下面將會介紹目標(biāo)追蹤算法中最簡單的一種 —— 模板匹配算法。 三、模板匹配算法 模板匹配
    發(fā)表于 10-28 07:21

    使用CubeMX移植nano編譯時提示大量未聲明,為什么?

    編譯時出現(xiàn)大量報錯。報錯集中在core/src/syscalls.c以及sysmem.c文件中。 報錯內(nèi)容如下所示,主要是提示找不到errno.h中的相關(guān)聲明。 但是errno.h使用f12可以打開
    發(fā)表于 09-26 06:29

    【匯思博SEEK100開發(fā)板試用體驗】3/第一次使用OpenHarmony

    本分享貼,聚焦第一次上手的時候,可能出現(xiàn)的一些問題,希望對其他小伙伴也有幫助。 ??一、創(chuàng)建首個OpenHarmony工程?? ??選擇開發(fā)范式?? ??聲明式開發(fā)(eTS)??:主流推薦,高性能
    發(fā)表于 08-20 22:21

    基于LockAI視覺識別模塊:C++人臉識別

    人臉模板進(jìn)行比較來實現(xiàn)識別。 2. C++ API 文檔 2.1 FaceRecognitionSystem 2.1.1 頭文件 #include <
    發(fā)表于 07-01 12:01

    在IAR Arm開發(fā)工具鏈中--function_sections編譯選項的使用

    本文主要介紹在IAR Arm開發(fā)工具鏈中不修改源代碼的情況下使用??function_sections編譯選項把函數(shù)放到單獨的section。
    的頭像 發(fā)表于 06-13 13:53 ?1395次閱讀
    在IAR Arm開發(fā)工具鏈中--<b class='flag-5'>function</b>_sections編譯選項的使用

    HarmonyOS5云服務(wù)技術(shù)分享--Serverless抽獎模板部署

    手把手教你部署HarmonyOS Serverless抽獎活動模板(附貼心提醒) 嘿,小伙伴們!今天給大家分享一個超實用的教程——如何用華為HarmonyOS的Serverless模板快速搭建抽獎
    發(fā)表于 05-22 20:25

    基于LockAI視覺識別模塊:C++多模板匹配

    ; ? using namespace cv; using namespace std; ? // 多模板匹配函數(shù)(支持彩色或灰度圖像,僅繪制匹配度最高的框) void
    發(fā)表于 05-14 15:00

    基于LockAI視覺識別模塊:C++多模板匹配

    模板匹配是一種在圖像中同時尋找多個模板的技術(shù)。通過對每個模板逐一進(jìn)行匹配,找到與輸入圖像最相似的區(qū)域,并標(biāo)記出匹配度最高的結(jié)果。本實驗提供了一個簡單的多模板匹配案例,并將其封裝為一個
    的頭像 發(fā)表于 05-14 14:37 ?1253次閱讀
    基于LockAI視覺識別模塊:C++多<b class='flag-5'>模板</b>匹配

    基于LockAI視覺識別模塊:C++模板匹配

    ; #include <iostream> ? using namespace cv; using namespace std; ? // 模板匹配函數(shù) bool
    發(fā)表于 05-13 14:40

    基于LockAI視覺識別模塊:C++模板匹配

    模板匹配是一種在圖像中尋找特定模式的技術(shù)。它通過滑動一個模板圖像(較小的圖像)在輸入圖像上進(jìn)行比較,找到最相似的區(qū)域。本實驗提供了一個簡單的模板匹配案例,并將其封裝為一個自定義函數(shù)performTemplateMatching,
    的頭像 發(fā)表于 05-13 14:14 ?443次閱讀
    基于LockAI視覺識別模塊:C++<b class='flag-5'>模板</b>匹配

    KiCad 9 探秘(六):如何用&quot;器件&quot;玩轉(zhuǎn)多通道設(shè)計與自定義DRC規(guī)則

    “ ?老版本的 KiCad 只有網(wǎng)絡(luò)(Net Class),但沒有器件(Component Class),在 KiCad 9 中器件終于亮相了。結(jié)合規(guī)則區(qū)域,器件可以在多通道設(shè)
    的頭像 發(fā)表于 02-08 11:14 ?2270次閱讀
    KiCad 9 探秘(六):如何用&quot;器件<b class='flag-5'>類</b>&quot;玩轉(zhuǎn)多通道設(shè)計與自定義DRC規(guī)則

    圖紙模板中的文本變量

    “ ?文本變量和系統(tǒng)自帶的內(nèi)置變量,可以幫助工程師靈活、高效地配置標(biāo)題欄中的信息,而不用擔(dān)心模板中的文字對象被意外修改。 ? ” 文本變量的語法 文本變量以?${VARIABLENAME}?的方式
    的頭像 發(fā)表于 11-13 18:21 ?1032次閱讀
    圖紙<b class='flag-5'>模板</b>中的文本變量

    A0到A4的圖框只要一個圖紙模板就搞定了?

    “ ?圖紙模板規(guī)范了圖紙的尺寸大小,同時可以在標(biāo)題欄顯示與圖紙相關(guān)的信息,如產(chǎn)品名稱、版本、日期等。從標(biāo)準(zhǔn)化的角度考慮,公司通常會定義A0~A4的圖紙模板,用于不同的設(shè)計場合。KiCad提供了一種
    的頭像 發(fā)表于 11-13 18:13 ?2228次閱讀
    A0到A4的圖框只要一個圖紙<b class='flag-5'>模板</b>就搞定了?

    手寫圖像模板匹配算法在OpenCV中的實現(xiàn)

    OpenCV中的模板匹配是支持基于NCC相似度查找的,但是不是很好用,一個主要的原因是查找最大閾值,只能匹配一個,自己比對閾值,又導(dǎo)致無法正確設(shè)定閾值范圍,所以問題很多。于是我重新寫了純Python版本的NCC圖像模板匹配的代碼實現(xiàn)了一個Python版本的,簡單易用,支持
    的頭像 發(fā)表于 11-11 10:12 ?1013次閱讀
    手寫圖像<b class='flag-5'>模板</b>匹配算法在OpenCV中的實現(xiàn)