STL常用函式

2021-07-22 01:19:33 字數 3030 閱讀 8499

1.map中查詢指定鍵值

map.find()

2.set中查詢指定鍵值

sets;

s.find()

s.count()

注:count()函式統計某一鍵值出現的次數,因此可以檢視set中是否出現某一鍵值。

3.sort()函式

sort(vect.begin(), vect.end());//此時相當於呼叫

sort(vect.begin(), vect.end(), less() );

sort 中的其他比較函式

equal_to 相等

not_equal_to 不相等

less 小於

greater 大於

less_equal 小於等於

greater_equal 大於等於

上述例子中系統 自己為sort提供了less仿函式。在stl中還提供了其他仿函 數,以下是仿函式列表: 不能直接寫入仿函式的名字,而是要寫其過載的()函式: less();

當你的容器中元 素時一些標準型別(int float char)或者string時,你可以直 接使用這些函式模板。但如果你時自己定義的型別或者你需要按照其他方式排序,你可以有兩種方法來達到效果:一種是自己寫比較函式。另一種是過載型別的'<'操作賦。

區域性排序其實是為了減少不必要的操作而提供的排序方式。

其函式原型為:

void partial_sort(randomaccessiterator first, randomaccessiterator middle,randomaccessiterator last);

void partial_sort(randomaccessiterator first,randomaccessiterator middle,randomaccessiterator last, strictweakordering comp);

例如:班上有1000個學生,我想知道分數最低的5名是哪些人。

partial_sort(vect.begin(),vect.begin()+5,vect.end(),less());

effective stl對排序函式的總結:

若需對vector, string, deque, 或array容器進行全排序,你可選擇sort或stable_sort;

若只需對vector, string, deque, 或array容器中取得top n的元素,部分排序partial_sort是首選.

若對於vector, string, deque, 或array容器,你需要找到第n個位置的元素或者你需要得到top n且不關係top n中的內部 順序,nth_element是最 理想的;

若你需要從標準序列容器或者array中把滿足某個條件 或者不滿足某個條件的元素分開,你最好使用partition或stable_partition;

若使用的list容器,你可以直接使用partition和stable_partition演算法,你可以使用list::sort代替sort和stable_sort排序。

4.find()函式

find( v1.begin(), v1.end(), num_to_find );

利用返回布林值的謂詞判斷pred,檢查迭代器區間[first,last)(閉開區間)上的每乙個元素,如果迭代器i滿足pred(*i)=true,表示找到元素並返回迭代值i(找到的第乙個符合條件的元素);未找到元素,返回末位置last。函式原型:find_if(v.begin(),v.end(),divby5);

bool isodd (int i)

std::vector::iterator it = std::find_if (myvector.begin(), myvector.end(), isodd);

5.count()函式

listl;

count(l.begin(),l.end(),value)

count_if(l.begin(),l.end(),pred)。謂詞pred含義同find_if中的謂詞。

6.search()函式

search演算法函式在乙個序列中搜尋與另一串行匹配的子串行。引數分別為乙個序列的開始位置,結束位置和另乙個序列的開始,結束位置。

vector::iterator ilocation;

ilocation=search(v1.begin(),v1.end(),v2.begin(),v2.end());

search_n演算法函式搜尋序列中是否有一系列元素值均為某個給定值的子串行。函式原型:search_n(v.begin(),v.end(),3,8),在v中找到3個連續的元素8。

最後乙個子串行搜尋find_end

函式原型find_end(v1.begin(),v1.end(),v2.begin(),v2.end());在v1中要求的位置查詢v2中要求的序列。

7.copy()函式

copy(v.begin(),v.end(),l.begin());將v中的元素複製到l中。

transform(v.begin(),v.end(),l.begin(),square);也是複製,但是要按某種方案複製。

int square(int x)

8.replace()

replace演算法將指定元素值替換為新值。

replace(v.begin(),v.end(),25,100);將元素範圍中的25替換為100

函式原型:replace_if(v.begin(),v.end(),odd,100);

bool odd(int x)

9.fill_n()函式

函式原型fill_n(v.begin(),5,-1);向從v.begin開始的後面5個位置跳入-1

generate_n(v.begin(),5,rand);向從v.begin開始的後面5個位置隨機填寫資料。

10.remove_if()

返回值相當於移除滿足條件的元素後形成的新向量的end()值。

函式原型:remove_if(v.begin(),v.end(),even);

11.unique

剔除連續重複元素unique

unique(v.begin(),v.end());

STL 常用函式

參考 1 count系列 1 int count iterator first,iterator last,val 2 int count if iterator first,iterator last,pred fun 2 copy系列 1 iterator copy iterator first...

STL中常用函式

標頭檔案寫 include 定義 stack stackname 成員函式 成員函式 功能bool empty 棧為空返回true,否則返回false void pop 刪除棧頂元素,即出棧 void push const type val 將新元素val進棧,使其成為棧頂元素 type top 檢...

STL常用函式總結

先進後出,只有乙個出口,只能操作最頂端元素。定義stackstack name 例 stack int s 操作s.empty 返回bool型,表示棧內是否為空 s.size 返回棧內元素個數 s.pop 移除棧頂元素 s.push a 向棧中壓入a元素 s.top 返回棧頂元素 先進先出,從底端加...