C++常用函數(shù)總結(jié)(algorithm 頭文件)
std::sort
std::sort 函數(shù)用于對(duì)數(shù)組或容器進(jìn)行排序,可以按照默認(rèn)的升序排序或指定比較函數(shù)進(jìn)行排序。
語(yǔ)法如下:
template <class RandomAccessIterator> void sort(RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- first:待排序元素的起始地址。
- last:待排序元素的終止地址,不包含在排序范圍內(nèi)。
- comp:可選參數(shù),比較函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> int main() { int arr[] = {4, 1, 8, 3, 2, 5}; int n = sizeof(arr) / sizeof(int); std::sort(arr, arr + n); // 對(duì)數(shù)組進(jìn)行升序排序 for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
輸出結(jié)果為:
1 2 3 4 5 8
std::max 和 std::min
std::max 和 std::min 函數(shù)用于獲取兩個(gè)值中的最大值或最小值。
語(yǔ)法如下:
template <class T> const T& max(const T& a, const T& b); template <class T, class Compare> const T& max(const T& a, const T& b, Compare comp); template <class T> const T& min(const T& a, const T& b); template <class T, class Compare> const T& min(const T& a, const T& b, Compare comp);
- a:待比較的第一個(gè)值。
- b:待比較的第二個(gè)值。
- comp:可選參數(shù),比較函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> int main() { int a = 3, b = 5; int max_val = std::max(a, b); // 獲取 a 和 b 中的最大值 int min_val = std::min(a, b); // 獲取 a 和 b 中的最小值 std::cout << "max_val = " << max_val << std::endl; std::cout << "min_val = " << min_val << std::endl; return 0; }
輸出結(jié)果為:
max_val = 5
min_val = 3
std::binary_search
std::binary_search 函數(shù)用于在已排序的數(shù)組或容器中搜索元素,返回值為布爾類(lèi)型。
語(yǔ)法如下:
template <class ForwardIterator, class T> bool binary_search(ForwardIterator first, ForwardIterator last, const T& value); template <class ForwardIterator, class T, class Compare> bool binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
- first:待搜索區(qū)間的起始地址。
- last:待搜索區(qū)間的終止地址,不包含在搜索范圍內(nèi)。
- value:待搜索的值。
- comp:可選參數(shù),比較函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(int); bool found = std::binary_search(arr, arr + n, 3); // 在數(shù)組中搜索值為 3 的元素 if (found) { std::cout << "Found" << std::endl; } else { std::cout << "Not found" << std::endl; } return 0; }
輸出結(jié)果為:
Found
std::count
std::count 函數(shù)用于統(tǒng)計(jì)容器或數(shù)組中指定值的個(gè)數(shù)。
語(yǔ)法如下:
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& value);
- first:待統(tǒng)計(jì)區(qū)間的起始地址。
- last:待統(tǒng)計(jì)區(qū)間的終止地址,不包含在統(tǒng)計(jì)范圍內(nèi)。
- value:待統(tǒng)計(jì)的值。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 3, 3, 4, 5}; int cnt = std::count(vec.begin(), vec.end(), 3); // 統(tǒng)計(jì)容器中值為 3 的元素個(gè)數(shù) std::cout << "count = " << cnt << std::endl; return 0; }
輸出結(jié)果為:
count = 3
std::accumulate
std::accumulate 函數(shù)用于計(jì)算容器或數(shù)組中所有元素的和,也可以指定一個(gè)初始值。
語(yǔ)法如下:
template <class InputIterator, class T> T accumulate(InputIterator first, InputIterator last, T init); template <class InputIterator, class T, class BinaryOperation> T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
- first:待求和區(qū)間的起始地址。
- last:待求和區(qū)間的終止地址,不包含在求和范圍內(nèi)。
- init:可選參數(shù),求和的初始值。
- binary_op:可選參數(shù),二元運(yùn)算函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::accumulate(vec.begin(), vec.end(), 0); // 求和 std::cout << "sum = " << sum << std::endl; return 0; }
輸出結(jié)果為:
sum = 15
std::for_each
std::for_each 函數(shù)用于對(duì)容器或數(shù)組中的每個(gè)元素執(zhí)行指定的操作,可以使用函數(shù)指針或函數(shù)對(duì)象來(lái)指定操作。
語(yǔ)法如下:
template <class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn);
- first:待遍歷區(qū)間的起始地址。
- last:待遍歷區(qū)間的終止地址,不包含在遍歷范圍內(nèi)。
- fn:函數(shù)指針或函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> void print(int x) { std::cout << x << " "; } int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::for_each(vec.begin(), vec.end(), print); // 對(duì)容器中的每個(gè)元素執(zhí)行 print 操作 std::cout << std::endl; return 0; }
輸出結(jié)果為:
1 2 3 4 5
std::unique
std::unique 函數(shù)用于去除容器或數(shù)組中的重復(fù)元素,僅保留第一個(gè)出現(xiàn)的元素。
語(yǔ)法如下:
template <class ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last); template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- first:待去重區(qū)間的起始地址。
- last:待去重區(qū)間的終止地址,不包含在去重范圍內(nèi)。
- pred:可選參數(shù),二元謂詞函數(shù)對(duì)象。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 3, 4, 4, 5}; auto last = std::unique(vec.begin(), vec.end()); // 去除容器中的重復(fù)元素 vec.erase(last, vec.end()); // 刪除重復(fù)的元素 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結(jié)果為:
1 2 3 4 5
std::reverse
std::reverse 函數(shù)用于將容器或數(shù)組中的元素進(jìn)行反轉(zhuǎn)。
語(yǔ)法如下:
template <class BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last);
- first:待反轉(zhuǎn)區(qū)間的起始地址。
- last:待反轉(zhuǎn)區(qū)間的終止地址,不包含在反轉(zhuǎn)范圍內(nèi)。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::reverse(vec.begin(), vec.end()); // 反轉(zhuǎn)容器中的元素 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結(jié)果為:
5 4 3 2 1
std::fill
std::fill 函數(shù)用于將指定區(qū)間的元素賦值為指定的值。
語(yǔ)法如下:
template <class ForwardIterator, class T> void fill(ForwardIterator first, ForwardIterator last, const T& value);
- first:要填充的第一個(gè)元素的迭代器。
- last:要填充的最后一個(gè)元素的迭代器,不包括在范圍內(nèi)。
- value:要填充的值。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec(10); // 創(chuàng)建一個(gè)包含 10 個(gè)元素的向量 std::fill(vec.begin(), vec.end(), 3); // 將向量中的所有元素都賦值為 3 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結(jié)果為:
3 3 3 3 3 3 3 3 3 3
另外需要注意的是,如果使用 fill 函數(shù)填充的元素是自定義類(lèi)型,需要自行定義該類(lèi)型的賦值運(yùn)算符重載函數(shù)。
std::next_permutation
std::next_permutation 函數(shù)用于求出容器或數(shù)組中元素的下一個(gè)排列組合。
std::next_permutation 是`cpp 標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù),用于返回一組元素的下一個(gè)排列。下面是該函數(shù)的定義:
template<class BidirIt> bool next_permutation(BidirIt first, BidirIt last);
該函數(shù)接受兩個(gè)迭代器作為參數(shù),表示需要進(jìn)行排列的元素范圍。
函數(shù)會(huì)嘗試將這些元素重新排列為下一個(gè)字典序更大的排列,如果成功,則返回 true,否則返回 false。
下面是一個(gè)示例:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec{1, 2, 3}; do { for (auto i : vec) { std::cout << i << ' '; } std::cout << '\n'; } while (std::next_permutation(vec.begin(), vec.end())); return 0; }
輸出為:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
在這個(gè)示例中,我們使用了 std::next_permutation 函數(shù)對(duì) vec 進(jìn)行了排列,并使用了一個(gè) do-while 循環(huán)來(lái)重復(fù)輸出排列結(jié)果,直到排列到了最后一個(gè)排列為止。
std::partition
std::partition 函數(shù)用于將容器或數(shù)組中的元素按照指定的條件進(jìn)行分區(qū),使得滿足條件的元素排在不滿足條件的元素之前。
std::partition是一個(gè)標(biāo)準(zhǔn)庫(kù)函數(shù),可以將一組元素重新排序,使得滿足某個(gè)特定條件的元素在序列的前面,而不滿足條件的元素在后面。該函數(shù)接受三個(gè)參數(shù):
template <typename ForwardIt, typename UnaryPredicate> ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p);
第一個(gè)參數(shù)是指向第一個(gè)元素的迭代器,第二個(gè)參數(shù)是指向最后一個(gè)元素后面的迭代器(即last不在范圍內(nèi)),第三個(gè)參數(shù)是一個(gè)一元謂詞(即只接受一個(gè)參數(shù)的函數(shù)對(duì)象)。
使用該函數(shù)時(shí),需要提供一個(gè)一元謂詞,該謂詞定義了一個(gè)條件,使得滿足該條件的元素在序列的前面,而不滿足條件的元素在后面。函數(shù)返回一個(gè)迭代器,該迭代器指向最后一個(gè)滿足條件的元素后面的位置。在返回的迭代器之前的元素都滿足條件,而在迭代器之后的元素都不滿足條件。
下面是一個(gè)使用std::partition函數(shù)的示例代碼:
#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9}; auto is_odd = [](int x) { return x % 2 == 1; }; auto it = std::partition(v.begin(), v.end(), is_odd); std::cout << "Odd numbers: "; std::copy(v.begin(), it, std::ostream_iterator<int>(std::cout, " ")); std::cout << "\nEven numbers: "; std::copy(it, v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; return 0; }
該代碼使用了lambda表達(dá)式is_odd作為一元謂詞,該謂詞返回true表示元素是奇數(shù),返回false表示元素是偶數(shù)。函數(shù)將一個(gè)包含1到9的整數(shù)序列重新排序,使得奇數(shù)在前面,偶數(shù)在后面,并打印出結(jié)果
輸出結(jié)果為:
Odd numbers: 1 3 5 7 9
Even numbers: 2 4 6 8
可以看到,std::partition函數(shù)將奇數(shù)放在了序列的前面,偶數(shù)放在了后面。返回的迭代器it指向序列中最后一個(gè)奇數(shù)的后面一個(gè)位置。函數(shù)std::copy將奇數(shù)和偶數(shù)分別打印出來(lái)。
注:lambda表達(dá)式`cpp11中新增的特性:
`cpp11中,可以使用lambda表達(dá)式來(lái)定義一個(gè)匿名函數(shù),語(yǔ)法格式如下:
[capture list] (parameters) -> return-type { function body }
其中,capture list 用于捕獲外部變量,parameters 是函數(shù)的參數(shù)列表,return-type 是函數(shù)的返回類(lèi)型(可以省略,由編譯器自動(dòng)推斷),function body 是函數(shù)體
以下是一個(gè)簡(jiǎn)單的示例,演示如何使用lambda表達(dá)式來(lái)計(jì)算兩個(gè)整數(shù)的和:
#include <iostream> int main() { int a = 5, b = 3; auto sum = [](int x, int y) -> int { return x + y; }; std::cout << "The sum of " << a << " and " << b << " is " << sum(a, b) << std::endl; return 0; }
在上面的示例中,我們使用lambda表達(dá)式定義了一個(gè)函數(shù)sum,它接受兩個(gè)int類(lèi)型的參數(shù)并返回它們的和。我們將該lambda表達(dá)式賦值給變量sum,并通過(guò)調(diào)用該變量來(lái)調(diào)用該函數(shù)。
capture list是lambda表達(dá)式中的一個(gè)可選項(xiàng),它允許我們?cè)诤瘮?shù)體中捕獲外部變量。
外部變量:在函數(shù)體外部定義的變量,可以是全局變量,也可以是局部變量。(捕獲外部變量的主要原因是 lambda 表達(dá)式是一個(gè)匿名函數(shù),它沒(méi)有自己的名稱和作用域,因此無(wú)法像常規(guī)函數(shù)那樣直接訪問(wèn)外部作用域中的變量。通過(guò)捕獲外部變量,lambda 表達(dá)式可以獲得對(duì)這些變量的訪問(wèn)權(quán),使其能夠在函數(shù)體中使用這些變量。)
捕獲可以按值或按引用進(jìn)行,這決定了 lambda 表達(dá)式如何訪問(wèn)外部變量。按值捕獲將外部變量的值復(fù)制到 lambda 表達(dá)式的閉包中,這意味著 lambda 表達(dá)式不會(huì)影響外部變量的值。按引用捕獲將外部變量的引用傳遞給 lambda 表達(dá)式,這意味著 lambda 表達(dá)式可以更改外部變量的值。
例如,我們可以使用以下lambda表達(dá)式來(lái)將一個(gè)整數(shù)加上一個(gè)固定的值:
#include <iostream> int main() { int a = 5, b = 3; int offset = 10; auto add_offset = [offset](int x) -> int { return x + offset; }; std::cout << "The result of adding " << offset << " to " << a << " is " << add_offset(a) << std::endl; std::cout << "The result of adding " << offset << " to " << b << " is " << add_offset(b) << std::endl; return 0; }
在上面的示例中,我們使用capture list來(lái)捕獲變量offset,并在lambda表達(dá)式中使用該變量。這樣,我們就可以使用該lambda表達(dá)式來(lái)將任何整數(shù)加上偏移量offset。
當(dāng)然,你可以將變量作為參數(shù)直接傳遞給 lambda 表達(dá)式,但這種方式通常適用于較簡(jiǎn)單的場(chǎng)景。在某些情況下,捕獲外部變量可以更方便地訪問(wèn)外部作用域中的變量,并允許 lambda 表達(dá)式使用外部變量的狀態(tài)來(lái)執(zhí)行更復(fù)雜的操作。
以上是 algorithm 頭文件中最常用的函數(shù)及其使用方法,當(dāng)然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),讀者可以通過(guò)查看 algorithm 頭文件的文檔來(lái)了解更多。
到此這篇關(guān)于C++常用函數(shù)的文章就介紹到這了,更多相關(guān)C++常用函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++頭文件algorithm中的函數(shù)功能詳解
- 簡(jiǎn)單談?wù)凜++ 頭文件系列之(algorithm)
- 詳解C++中的萬(wàn)能頭文件
- 關(guān)于VS2022不能使用<bits/stdc++.h>的解決方案(萬(wàn)能頭文件)
- C++ Boost Algorithm算法超詳細(xì)精講
- C++實(shí)現(xiàn)分水嶺算法(Watershed Algorithm)
- C++詳細(xì)講解常用math函數(shù)的用法
- C++常用字符串函數(shù)大全(2)
- 詳解C++字符串常用操作函數(shù)(查找、插入、截取、刪除等)
- c++中的string常用函數(shù)用法總結(jié)
相關(guān)文章
C++?OpenCV實(shí)現(xiàn)物體尺寸測(cè)量示例詳解
本文主要介紹了利用OpenCV對(duì)物體的尺寸進(jìn)行測(cè)量,即先定位到待測(cè)物體的位置,然后測(cè)量物體的寬高。感興趣的同學(xué)可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-01-01使用用C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼
大家好,本篇文章主要講的是使用用C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,歡迎借鑒學(xué)習(xí)C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼2021-12-12C++實(shí)現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法
這篇文章主要介紹了C++實(shí)現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法,是Windows應(yīng)用程序設(shè)計(jì)中非常實(shí)用的方法,需要的朋友可以參考下2014-10-10C++計(jì)算任意兩個(gè)日期天數(shù)差的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)任意兩個(gè)日期天數(shù)差,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-02-02實(shí)戰(zhàn)開(kāi)發(fā)為單片機(jī)的按鍵加一個(gè)鎖防止多次觸發(fā)的細(xì)節(jié)
今天小編就為大家分享一篇關(guān)于實(shí)戰(zhàn)開(kāi)發(fā)為單片機(jī)的按鍵加一個(gè)鎖防止多次觸發(fā)的細(xì)節(jié),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03