STL演算法 Algorithms 極值

2021-08-27 07:01:01 字數 1895 閱讀 3210

stl演算法(algorithms):極值 1、

min:返回兩個兩個引數中的最小值

原型:template const t& min ( const t& a, const t& b );

template

const t& min ( const t& a, const t& b, compare comp );

示例:// min example

#include

#include

using namespace std;

int main () 2、

max:返回兩個引數中的大值

原型:template const t& max ( const t& a, const t& b );

template

const t& max ( const t& a, const t& b, compare comp );

示例:// max example

#include

#include

using namespace std;

int main () 3、

min_element:返回(迭代器)指定範圍內的最小元素

原型:// min_element/max_element

#include

#include

using namespace std;

bool myfn(int i, int j) ; cout << "the 3! possible permutations with 3 elements:\n"; sort (myints,myints+3); do while ( next_permutation (myints,myints+3) ); return 0;}

6、prev_permutation:和next_permutation功能類似,返回的是序列中(所有元素)前乙個組合

原型:

template bool prev_permutation (bidirectionaliterator first,                         bidirectionaliterator last );template bool prev_permutation (bidirectionaliterator first,                         bidirectionaliterator last, compare comp);

示例:
// prev_permutation#include #include using namespace std;int main () ;  cout << "the 3! possible permutations with 3 elements:\n";  sort (myints,myints+3);  reverse (myints,myints+3);  do  while ( prev_permutation (myints,myints+3) );  return 0;}

7、lexicographical_compare:字典比較(針對的是兩個序列,返回的是布林值)

原型:template bool lexicographical_compare ( inputiterator1 first1, inputiterator1 last1,

inputiterator2 first2, inputiterator2 last2 );
template
bool lexicographical_compare ( inputiterator1 first1, inputiterator1 last1,
inputiterator2 first2, inputiterator2 last2,
compare comp );

演算法 algorithms 基礎之 選擇排序

演算法 algorithms 基礎之 選擇排序 沒那麼簡單的部落格 陣列a中有n個數,首先找出a中的最小元素並將其與a 1 中的元素進行交換。接著,找出a中的次最小原始並將其與a 2 中的元素進行交換。對a中前n 1個元素按該方式繼續。該演算法稱為選擇演算法。假定陣列a 5,2,4,6,1,3 是需...

演算法 algorithms 基礎之 插入排序

演算法 algorithms 基礎之 插入排序 沒那麼簡單的部落格 插入排序,對於少量的元素的資料,它是乙個有效的演算法。它的工作方式就像許多人排序一副撲克牌。開始時,我們的左手為空並且桌子上的牌面向下。然後,我們每次從桌上拿走一張牌並將它插入左手中正確的位置。為了找到一張牌的正確位置,我們從右往左...

Algorithms 最大子串行和

最大連續子數列和一道很經典的演算法問題,給定乙個數列,其中可能有正數也可能有負數,我們的任務是找出其中連續的乙個子數列 不允許空數列 使它們的和盡可能大。我們一起用多種方式,逐步優化解決這個問題。為了更清晰的理解問題,首先我們先看一組資料 8 2 6 1 5 4 7 2 3 第一行的8是說數列的長度...