關于STL的查找算法是不是有很多小伙伴們都不是非常的了解呢?那么我們今天就帶著這個疑問一起去看看stl常用的查找算法,感興趣的小伙伴們一起來看看吧。
?
《effective STL》中有句忠告,盡量用算法替代手寫循環;
查找有三種,即點線面:
點就是查找目標為單個元素;
線就是查找目標為區間;
面就是查找目標為集合;
針對每個類別的查找,默認的比較函數是相等,為了滿足更豐富的需求,算法也都提供了自定義比較函數的版本;
單個元素查找
find() 比較條件為相等的查找
find()從給定區間中查找單個元素,定義:
?
?
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
?
示例,從myvector中查找30:
?
?
int myints[] = { 10, 20, 30, 40 };
std::vector<int> myvector (myints,myints+4);
it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
??? std::cout << "Element found in myvector: " << *it << '/n';
else
??? std::cout << "Element not found in myvector/n";
?
find_if() 自定義比較函數
std::find_if():從給定區間中找出滿足比較函數的第一個元素;
示例,從myvector中查找能夠被30整除的第一個元素:
?
?
bool cmpFunction (int i) {
? return ((i%30)==0);
}
it = std::find_if (myvector.begin(), myvector.end(), cmpFunction);
std::cout << "first:" <<? *it <<std::endl;
?
count() 統計元素出現次數
std::count():統計區間中某個元素出現的次數;
std:count_if():count()的自定義比較函數版本
search_n() 查詢單個元素重復出現的位置
search_n(): find用來查詢單個元素,search_n則用來查找區間中重復出現n次的元素;
示例:查詢myvector中30連續出現2次的位置:
?
?
int myints[]={10,20,30,30,20,10,10,20};
std::vector<int> myvector (myints,myints+8);
it = std::search_n (myvector.begin(), myvector.end(), 2, 30);
?
search_n() 支持自定義比較函數;
adjacent_find() 查詢區間中重復元素出現的位置
adjacent_find() 查詢區間中重復元素出現的位置,該算法支持自定義比較函數;
lower_bound() 有序區間中查詢元素邊界
lower_bound()用來在一個排序的區間中查找第一個不小于給定元素的值:
示例:查找容器v中不小于20的下界:
?
?
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8);?????????? // 10 20 30 30 20 10 10 20
std::sort (v.begin(), v.end());??????????????? // 10 10 10 20 20 20 30 30
std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), 20);
std::cout << "lower_bound at position " << (low- v.begin()) << '/n';
?
類似算法有upper_bound(),查找有序區間中第一個大于給定元素的值;
還有equal_range(),查找有序區間的上下邊界;(一次返回lower_bound()和upper_bound());
binary_search() 有序區間的二分查找
binary_search() 用來在一個有序區間中使用二分法查找元素是否在這個區間中,注,這個算法的返回值為bool,
不是下標位置,其內部的算法邏輯和lower_bound()相似,行為表現為:
?
?
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));
}
?
示例:從有序區間v中找3是否存在:
?
?
int myints[] = {1,2,3,4,5,4,3,2,1};
std::vector<int> v(myints,myints+9);???????????????????????? // 1 2 3 4 5 4 3 2 1
std::sort (v.begin(), v.end());
if (std::binary_search (v.begin(), v.end(), 3))
??? std::cout << "found!/n"; else std::cout << "not found./n";
?
min_element() 查找最小元素
min_element() 在給定區間中查找出最小值;
?
?
int myints[] = {3,7,2,5,6,4,9};
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '/n';
?
類似算法有:max_element() 查找最大值;
區間查找 search()
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);
?
find_end() 查找子區間最后一次出現的位置
search() 用來查找子區間第一次出現的位置,而find_end()用來查找子區間最后一次出現的位置:
find_end()支持自定義比較函數;
equal() 判斷兩個區間是否相等
equal()用來判斷兩個區間是否相等,該算法支持自定義比較函數;
mismatch() 查詢兩個區間首次出現不同的位置;
mismatch() 查詢兩個區間首先出現不同的位置,這個算法也支持自定義比較函數;
集合查找
find_first_of 查找集合中的任意一個元素
find_first_of()用來查找給定集合中的任意一個元素:
示例:從haystack中查找A,B,C出現的位置:
?
?
? int mychars[] = {'a','b','c','A','B','C'};
? std::vector<char> haystack (mychars,mychars+6);
? int needle[] = {'C','B','A'};
? // using default comparison:
? it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
?
find_first_of支持自定義比較函數;
以上所述就是小編為大家詳細介紹關于STL的查找算法,小編查找了不少循環遍歷,今天就為大家分享了STL查找算法的內容。