c bind1st 和 bind2nd的用法

2021-09-09 02:41:16 字數 1481 閱讀 3693

std::bind1st 和 std::bind2nd將二元函式轉換為一元函式,具體用法參加下面的**。

**介紹了兩種使用方式,第一種是使用std::less和std::greater,第二種是使用自定義的仿函式。

#include #include #include #include #include #include /**  

* std::bind1st std::bind2nd 就是將乙個二元函式的乙個引數設定為定值,這樣二元函式就轉換為了一元函式

* 因為有些演算法的引數要求必須是一元函式,但是我們又想用二元函式,那麼就可以使用這兩個函式

*//**

*@brief std::less 仿函式的內部實現

template struct less : binary_function };

void disp(int val); vecp.push_back(p1);

person p2 = ; vecp.push_back(p2);

person p3 = ; vecp.push_back(p3);

std::vectorvecret;

std::copy_if(vecp.begin(),vecp.end(),std::back_inserter(vecret),std::bind2nd(person_filter_func(),"ja")); //將包含關鍵字"ja"的person,複製到vecret容器中

std::for_each(vecret.begin(),vecret.end(),disp_v);//1, jack 3, jane

}

copy_if:

template outputiterator copy_if (inputiterator first, inputiterator last,

outputiterator result, unarypredicate pred)

++first;

} return result;

}

template binder1stbind1st (const operation& op, const t& x)

template class binder1st

: public unary_function

typename operation::result_type

operator() (const typename operation::second_argument_type& x) const

};

template forwarditerator remove_if (forwarditerator first, forwarditerator last,

unarypredicate pred)

++first;

} return result;

}

bind函式返回值 1 C bind詳解

給cpp開發者加星標,提公升c c 技能 可將std bind函式看作乙個通用的函式介面卡,它接受乙個可呼叫物件,生成乙個新的可呼叫物件來 適應 原物件的引數列表。std bind將可呼叫物件與其引數一起進行繫結,繫結後的結果可以使用std function儲存。以下是正文 先將可呼叫的物件儲存起來...

gh0st原始碼分析 gh0st 1

在gh0st classes中定義了一堆標頭檔案和他們的實現函式,先從inifile.h標頭檔案看起 定義了設定獲取檔名的函式,以及data的型別轉換函式和get函式 inifile.cpp 初始化檔案,設定檔名,型別為ini型別,並且設定格式的函式實現。gh0stdoc.h class cdocu...

啊哈,演算法自學記 1st

小哼的班上只有 5 個同學,這 5 個同學分別考了 5 分 3 分 5 分 2 分和 8 分,哎考得真是慘不忍睹 滿分是 10 分 接下來將分數進行從大到小排序,排序後是 8 5 5 3 2。include 滿分十分,就建立乙個11個資料的陣列,幾分出現幾次就在對應的陣列下標加一 最後,幾分出現幾次...