C STL 演算法精選之查詢篇

2021-09-07 21:59:44 字數 4845 閱讀 8069

1.查詢類演算法

adjacent_find(first,last);

查詢區間[first,last)內第一次出現連續的兩個相等的元素,並返回指向第乙個元素的迭代器,連續元素之間的比較,預設是==

adjacent_find(first,last,pred);

用途如上,但是元素之間的比較是通過函式pred來完成,pred接受兩個容器內元素型別的元素,返回bool值

函式原型:

template

forwarditerator adjacent_find ( forwarditerator first, forwarditerator last )

例子:// adjacent_find example

#include

#include

#include

using namespace std;

bool myfunction (int i, int j)

int main () ;

vectormyvector (myints,myints+8);

vector::iterator it;

// using default comparison:

it = adjacent_find (myvector.begin(), myvector.end());

if (it!=myvector.end())

cout << "the first consecutive repeated elements are: " << *it << endl;

//using predicate comparison:

it = adjacent_find (++it, myvector.end(), myfunction);

if (it!=myvector.end())

cout << "the second consecutive repeated elements are: " << *it << endl;

return 0;}

output:

the first consecutive repeated elements are: 30

the second consecutive repeated elements are: 10

find(first,last,value);

查詢區間[first,last)之間內值為value的元素,返回迭代器型別,若沒找到,則返回迭代器末尾end 函式原型:

template

inputiterator find ( inputiterator first, inputiterator last, const t& value )

例子:// find example

#include

#include

#include

using namespace std;

int main () ;

int * p;

// pointer to array element:

p = find(myints,myints+4,30);

++p;

cout << "the element following 30 is " << *p << endl;

vectormyvector (myints,myints+4);

vector::iterator it;

// iterator to vector element:

it = find (myvector.begin(), myvector.end(), 30);

++it;

cout << "the element following 30 is " << *it << endl;

return 0;}

output:

the element following 30 is 40

the element following 30 is 40

find_if(first,last,pred);

返回區間[first,last)內第乙個使pred函式返回為真的元素的迭代器,否則返回last注意:pred接受乙個引數函式原型:

template

inputiterator find_if ( inputiterator first, inputiterator last, predicate pred )

例子:// find_if example

#include

#include

#include

using namespace std;

bool isodd (int i)

int main ()

output:

the first odd value is 25

find_first_of(first1,last1,first2,last2);

find_first_of(first1,last1,first2,last2,pred);

返回乙個迭代器,使得[first2,last2)之中任意乙個元素第一次出現在區間[first1,last1)中。

預設比較為==,當然也可以自己定義pred函式(接受2個引數),返回bool型 函式原型:

template

forwarditerator1 find_first_of ( forwarditerator1 first1, forwarditerator1 last1,

forwarditerator2 first2, forwarditerator2 last2)

例子:// find_first_of example

#include

#include

#include

#include

using namespace std;

bool comp_case_insensitive (char c1, char c2)

int main () ;

vectormyvector (mychars,mychars+6);

vector::iterator it;

int match = ;

// using default comparison:

it = find_first_of (myvector.begin(), myvector.end(), match, match+3);

if (it!=myvector.end())

cout << "first match is: " << *it << endl;

// using predicate comparison:

it = find_first_of (myvector.begin(), myvector.end(),

match, match+3, comp_case_insensitive);

if (it!=myvector.end())

cout << "first match is: " << *it << endl;

return 0;}

output:

first match is: a

first match is: a

find_end(first1,last1,first2,last2);

find_end(first1,last1,first2,last2,pred);

返回乙個元素迭代器,使得在區間[first1,last1)中最後一次出現[fiest2,last2),

預設比較為==,當然也可以寫自己的比較函式pred,接受兩個引數,返回bool值函式原型:

template

forwarditerator1 find_end ( forwarditerator1 first1, forwarditerator1 last1,

forwarditerator2 first2, forwarditerator2 last2)

}++first1;

}return ret;

}例子:// find_end example

#include

#include

#include

using namespace std;

bool myfunction (int i, int j)

int main () ;

vectormyvector (myints,myints+10);

vector::iterator it;

int match1 = ;

// using default comparison:

it = find_end (myvector.begin(), myvector.end(), match1, match1+3);

if (it!=myvector.end())

cout << "match1 last found at position " << int(it-myvector.begin()) << endl;

int match2 = ;

// using predicate comparison:

it = find_end (myvector.begin(), myvector.end(), match2, match2+3, myfunction);

if (it!=myvector.end())

cout << "match2 last found at position " << int(it-myvector.begin()) << endl;

return 0;}

output:

match found at position 5

match found at position 3

C STL 之容器篇

stl 是標準模板庫,由容器,演算法,迭代器和容器介面卡組成。容器有 vector 陣列,順序儲存 list 鍊錶,可以翻轉,可以在頭尾新增,insert快,不可用 和at deque vector和list 的居中版,一部分順序,一部分用鍊錶的形式儲存,記憶體使用更加合理 map,set 關聯式容...

C STL的查詢演算法

假設你有乙個序列容器,或者有一對迭代器標識了乙個區間,現在你希望在容器中查詢一些資訊,這樣的查詢工作如何進行呢?你的選擇往往是 count,count if,find,find if,binary search,lower bound,upper bound,equal range.該如何選擇呢?現...

C STL 常用查詢演算法

find 查詢元素,內建型別 void test01 查詢在區間內出現的第乙個5,返回當前查詢元素迭代器 vector int iterator it find v.begin v.end 5 if it v.end else find 查詢元素,自定義型別,得告訴編譯器如何做對比 class pe...