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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL中的查找算法

發布時間:2025/4/16 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL中的查找算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????STL中有很多算法,這些算法可以用到一個或多個STL容器(因為STL的一個設計思想是將算法和容器進行分離),也可以用到非容器序列比如數組中。眾多算法中,查找算法是應用最為普遍的一類。

單個元素查找

1、 find() 比較條件為元素是否相等的查找:

template <class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val);

2、find_if() 自定義比較函數?
從給出的區間中查找第一個滿足比較函數的元素

bool cmpFunction (int i) {return ((i%30)==0); } it = std::find_if (myvector.begin(), myvector.end(), cmpFunction); std::cout << "first:" << *it <<std::endl;

3、count() 統計元素出現的次數?
std::count() 統計區間中某個元素出現的次數?
std::count_if() 自定義比較函數

4、min_element() 查找給定區間內最小值

5、max_element() 查找給定區間內最大值

6、binary_search() 有序區間的二分查找?
binary_search() 用來在一個有序區間中使用二分法查找元素是否在這個區間中,該算法的返回值為bool表示是否存在

template <class ForwardIterator, class T>bool binary_search (ForwardIterator first, ForwardIterator last, const T& val) {first = std::lower_bound(first,last,val);return (first!=last && !(val<*first)); }

?

7、lower_bound() 返回有序序列給定區間[first, last) (左閉右開)內的第一個大于等于value的位置。如果所有元素都小于value,則返回last。

template< class ForwardIterator, class Type > ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last, const Type &value );template< class ForwardIterator, class Type, class Compare> ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last, const Type &value, Compare comp ); //支持自定義比較函數

?

8、upper_bound() 返回有序序列給定區間[first, last) (左閉右開)內的第一個大于value的位置。如果所有元素都小于等于value,則返回last。

template< class ForwardIterator, class Type > ForwardIterator upper_bound( ForwardIterator first, ForwardIterator last, const Type &value );template< class ForwardIterator, class Type, class Compare> ForwardIterator upper_bound( ForwardIterator first, ForwardIterator last, const Type &value, Compare comp ); //支持自定義比較函數

?

其中lower_bound/upper_bound 可以用于任何支持隨機訪問的容器中,比如vector,deque,數組。對于不支持隨機訪問的容器如 set/map,這些容器有同名的方法來進行 lower_bound/upper_bound 操作。

map::lower_bound(key):返回map中第一個大于或等于key的迭代器指針 map::upper_bound(key):返回map中第一個大于key的迭代器指針 set::lower_bound(val):返回set中第一個大于或等于val的迭代器指針 set::upper_bound(val):返回set中第一個大于val的迭代器指針

?

區間查找(區間整體匹配)

1、search() 查找子區間首次出現的位置?
find() 用來查找單個元素,search() 用來查找一個子區間,比如 從myvector中查找自取件[20, 30] 的位置

int needle1[] = {20,30}; it = std::search (myvector.begin(), myvector.end(), needle1, needle1+2); if (it!=myvector.end()) std::cout << "needle1 found at position " << (it-myvector.begin()) << '\n';

?

search() 支持自定義比較函數,比如查詢給定區間中每個元素比目標區間小1的子區間:

bool cmpFunction (int i, int j) {return (i-j==1); } int myints[] = {1,2,3,4,5,1,2,3,4,5}; std::vector<int> haystack (myints,myints+10);int needle2[] = {1,2,3}; // using predicate comparison: it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, cmpFunction);

?

2、find_end() 查找子區間最后一次出現的位置?
search()查找子區間第一次出現的位置,而find_end() 用來查找子區間最后一次出現的位置,find_end()支持自定義比較函數。

3、equal() 判斷兩個區間是否相等

4、mismatch() 查詢兩個區間首次出現不同的位置

集合查找(集合內任意一個元素匹配)

find_first_of() 查找給定集合中的任意一個元素

總結

以上是生活随笔為你收集整理的STL中的查找算法的全部內容,希望文章能夠幫你解決所遇到的問題。

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