日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(10)chapter10 泛型算法 :谓词

發布時間:2025/3/21 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ Primer 5th笔记(10)chapter10 泛型算法 :谓词 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 定義

謂詞(predicate)是一個可調用的表達式,返回結果是一個能用作條件的值。

  • 用于一個對象或一個表達式,如果可以對其使用調用運算符,則稱為可調用的;
  • 可調用的對象有:函數、函數指針、重載函數調用運算符的類和lambda表達式。

2. n元謂詞

幾元謂詞接受幾元參數。 謂詞分為:一元謂詞和二元謂詞。

bool isShorter(const string &s1, const string &s2) {return s1.size() < s2.size(); }

eg. 使用謂詞的排序版本

vector<string> words = {"the", "quick", "red", "fox","red", "the","slow"}; cout << "Before: "; for(auto iter = words.begin();iter != words.end(); ++iter){cout << *iter << " "; } cout << endl; sort(words.begin(),words.end(), isShorter); //使用謂詞進行排序 cout << "After sort: "; for(const auto &s: words){cout << s << " "; } cout << endl;

輸出結果為:

Before: the quick red fox red the slow After sort: the red fox red the slow quick

3.應用舉例.

標準庫會為謂詞算法重新起個名字以表示跟原來不一樣。
eg

find (beg, end, val); // find first instance of val in the input rangefind_if (beg, end, pred); // find first instance for which pred is truereverse (beg, end);reverse_copy (beg, end, dest); 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的C++ Primer 5th笔记(10)chapter10 泛型算法 :谓词的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。