STL中常用的算法(尋找最大值,最小值,索引,區間內查找算法等等)

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

使用STL的Vector時,利用函數 max_element,min_element,distance可以獲取Vector中最大、最小值的值和位置索引:

代碼如下:

#include <algorithm>
#include <iostream>
 
int main()
{
    std::vector<double> v {1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0};
 
    std::vector<double>::iterator biggest = std::max_element(std::begin(v), std::end(v));
    //or std::vector<double>::iterator biggest = std::max_element(v.begin(), v.end);
 
    
    std::cout << "Max element is " << *biggest<< " at position " <<std::distance(std::begin(v), biggest) << std::endl;
    //另一方面,取最大位置也可以這樣來寫:
    //int nPos = (int)(std::max_element(v.begin(), v.end()) - (v.begin());
    //效果和採用distance(...)函數效果一致
    //說明:max_element(v.begin(), v.end()) 返回的是vector<double>::iterator, 
    //相當於指針的位置,減去初始指針的位置結果即爲最大值得索引。
 
    auto smallest = std::min_element(std::begin(v), std::end(v));
    std::cout << "min element is " << *smallest<< " at position " <<std::distance(std::begin(v), smallest) << std::endl;
}
 


 
輸出:
 
 

Max element is 5 at position 4
min element is 1 at position 0

當然還有求和

爲了對vector中的所有元素求和,我們利用accmulate這個函數。

頭文件#include <numeric>。
    int num=accumulate(va.begin(),va.end(),0); //求和

單個元素查找

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

1

2

template <class InputIterator, class T>

InputIterator find (InputIterator first, InputIterator last, const T& val);

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

1

2

3

4

5

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表示是否存在

1

2

3

4

5

6

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。

1

2

3

4

5

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。

1

2

3

4

5

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 操作。

1

2

3

4

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] 的位置

1

2

3

4

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的子區間:

1

2

3

4

5

6

7

8

9

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() 查找給定集合中的任意一個元素

 

參考:https://blog.csdn.net/leonardohaig/article/details/81484372 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章