STL演算法設計理念 謂詞,一元謂詞demo

2022-08-02 13:54:15 字數 766 閱讀 5719

謂詞:

一元函式物件:函式引數1個;

二元函式物件:函式引數2個;

一元謂詞 函式引數1個。函式返回值是bool型別,能夠作為乙個推斷式

謂詞能夠使乙個仿函式,也能夠是乙個**函式。

demo 一元謂詞

#include #include #include #include using namespace std;

template class isdiv

bool operator()(t &t) // 一元謂詞

protected:

private:

t divisor;

};void play01()

int a = 4;

isdivisdiv(a);

find_if(v.begin(), v.end(), isdiv);

//find_if(v.begin(), v.end(), isdiv(4)); // 也能夠這樣寫

/* find_if()函式原型

templateinline

_init find_if(_init _first, _init _last, _pr _pred)

*/// find_if的返回值值乙個迭代器

vector::iterator it = find_if(v.begin(), v.end(), isdiv);

if (it == v.end())

else

}int main()

find if一元謂詞

find if接受一元謂詞,因此傳遞給find if的可呼叫物件必須接受單一引數。舉個例子 vector vec auto wc find if vec.begin vec.end sz const string words 當中的lambda表示式 sz const string words 轉化...

C 一元謂詞和二元謂詞

include include 演算法標頭檔案 include include using namespace std 返回型別為bool型別 的operator struct greatstruct for each需要使用的偽函式 template typename t void printer...

函式物件,一元謂詞,二元謂詞,預定義函式物件

find if sort for each 的使用 for each v1.begin v1.end showelemt 要使用預定義函式物件需要包含 functional 標頭檔案vector iterator it find if v1.begin v1.end myint sort v3.be...