C++常用函數(shù)總結(algorithm 頭文件)
std::sort
std::sort 函數(shù)用于對數(shù)組或容器進行排序,可以按照默認的升序排序或指定比較函數(shù)進行排序。
語法如下:
template <class RandomAccessIterator> void sort(RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
- first:待排序元素的起始地址。
- last:待排序元素的終止地址,不包含在排序范圍內。
- comp:可選參數(shù),比較函數(shù)對象。
示例代碼如下:
#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); // 對數(shù)組進行升序排序 for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
輸出結果為:
1 2 3 4 5 8
std::max 和 std::min
std::max 和 std::min 函數(shù)用于獲取兩個值中的最大值或最小值。
語法如下:
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:待比較的第一個值。
- b:待比較的第二個值。
- comp:可選參數(shù),比較函數(shù)對象。
示例代碼如下:
#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; }
輸出結果為:
max_val = 5
min_val = 3
std::binary_search
std::binary_search 函數(shù)用于在已排序的數(shù)組或容器中搜索元素,返回值為布爾類型。
語法如下:
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ū)間的終止地址,不包含在搜索范圍內。
- value:待搜索的值。
- comp:可選參數(shù),比較函數(shù)對象。
示例代碼如下:
#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; }
輸出結果為:
Found
std::count
std::count 函數(shù)用于統(tǒng)計容器或數(shù)組中指定值的個數(shù)。
語法如下:
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& value);
- first:待統(tǒng)計區(qū)間的起始地址。
- last:待統(tǒng)計區(qū)間的終止地址,不包含在統(tǒng)計范圍內。
- value:待統(tǒng)計的值。
示例代碼如下:
#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)計容器中值為 3 的元素個數(shù) std::cout << "count = " << cnt << std::endl; return 0; }
輸出結果為:
count = 3
std::accumulate
std::accumulate 函數(shù)用于計算容器或數(shù)組中所有元素的和,也可以指定一個初始值。
語法如下:
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ū)間的終止地址,不包含在求和范圍內。
- init:可選參數(shù),求和的初始值。
- binary_op:可選參數(shù),二元運算函數(shù)對象。
示例代碼如下:
#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; }
輸出結果為:
sum = 15
std::for_each
std::for_each 函數(shù)用于對容器或數(shù)組中的每個元素執(zhí)行指定的操作,可以使用函數(shù)指針或函數(shù)對象來指定操作。
語法如下:
template <class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn);
- first:待遍歷區(qū)間的起始地址。
- last:待遍歷區(qū)間的終止地址,不包含在遍歷范圍內。
- fn:函數(shù)指針或函數(shù)對象。
示例代碼如下:
#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); // 對容器中的每個元素執(zhí)行 print 操作 std::cout << std::endl; return 0; }
輸出結果為:
1 2 3 4 5
std::unique
std::unique 函數(shù)用于去除容器或數(shù)組中的重復元素,僅保留第一個出現(xiàn)的元素。
語法如下:
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ū)間的終止地址,不包含在去重范圍內。
- pred:可選參數(shù),二元謂詞函數(shù)對象。
示例代碼如下:
#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()); // 去除容器中的重復元素 vec.erase(last, vec.end()); // 刪除重復的元素 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結果為:
1 2 3 4 5
std::reverse
std::reverse 函數(shù)用于將容器或數(shù)組中的元素進行反轉。
語法如下:
template <class BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last);
- first:待反轉區(qū)間的起始地址。
- last:待反轉區(qū)間的終止地址,不包含在反轉范圍內。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::reverse(vec.begin(), vec.end()); // 反轉容器中的元素 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結果為:
5 4 3 2 1
std::fill
std::fill 函數(shù)用于將指定區(qū)間的元素賦值為指定的值。
語法如下:
template <class ForwardIterator, class T> void fill(ForwardIterator first, ForwardIterator last, const T& value);
- first:要填充的第一個元素的迭代器。
- last:要填充的最后一個元素的迭代器,不包括在范圍內。
- value:要填充的值。
示例代碼如下:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> vec(10); // 創(chuàng)建一個包含 10 個元素的向量 std::fill(vec.begin(), vec.end(), 3); // 將向量中的所有元素都賦值為 3 for (auto x : vec) { std::cout << x << " "; } std::cout << std::endl; return 0; }
輸出結果為:
3 3 3 3 3 3 3 3 3 3
另外需要注意的是,如果使用 fill 函數(shù)填充的元素是自定義類型,需要自行定義該類型的賦值運算符重載函數(shù)。
std::next_permutation
std::next_permutation 函數(shù)用于求出容器或數(shù)組中元素的下一個排列組合。
std::next_permutation 是`cpp 標準庫中的一個函數(shù),用于返回一組元素的下一個排列。下面是該函數(shù)的定義:
template<class BidirIt> bool next_permutation(BidirIt first, BidirIt last);
該函數(shù)接受兩個迭代器作為參數(shù),表示需要進行排列的元素范圍。
函數(shù)會嘗試將這些元素重新排列為下一個字典序更大的排列,如果成功,則返回 true,否則返回 false。
下面是一個示例:
#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
在這個示例中,我們使用了 std::next_permutation 函數(shù)對 vec 進行了排列,并使用了一個 do-while 循環(huán)來重復輸出排列結果,直到排列到了最后一個排列為止。
std::partition
std::partition 函數(shù)用于將容器或數(shù)組中的元素按照指定的條件進行分區(qū),使得滿足條件的元素排在不滿足條件的元素之前。
std::partition是一個標準庫函數(shù),可以將一組元素重新排序,使得滿足某個特定條件的元素在序列的前面,而不滿足條件的元素在后面。該函數(shù)接受三個參數(shù):
template <typename ForwardIt, typename UnaryPredicate> ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p);
第一個參數(shù)是指向第一個元素的迭代器,第二個參數(shù)是指向最后一個元素后面的迭代器(即last不在范圍內),第三個參數(shù)是一個一元謂詞(即只接受一個參數(shù)的函數(shù)對象)。
使用該函數(shù)時,需要提供一個一元謂詞,該謂詞定義了一個條件,使得滿足該條件的元素在序列的前面,而不滿足條件的元素在后面。函數(shù)返回一個迭代器,該迭代器指向最后一個滿足條件的元素后面的位置。在返回的迭代器之前的元素都滿足條件,而在迭代器之后的元素都不滿足條件。
下面是一個使用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表達式is_odd作為一元謂詞,該謂詞返回true表示元素是奇數(shù),返回false表示元素是偶數(shù)。函數(shù)將一個包含1到9的整數(shù)序列重新排序,使得奇數(shù)在前面,偶數(shù)在后面,并打印出結果
輸出結果為:
Odd numbers: 1 3 5 7 9
Even numbers: 2 4 6 8
可以看到,std::partition函數(shù)將奇數(shù)放在了序列的前面,偶數(shù)放在了后面。返回的迭代器it指向序列中最后一個奇數(shù)的后面一個位置。函數(shù)std::copy將奇數(shù)和偶數(shù)分別打印出來。
注:lambda表達式`cpp11中新增的特性:
`cpp11中,可以使用lambda表達式來定義一個匿名函數(shù),語法格式如下:
[capture list] (parameters) -> return-type { function body }
其中,capture list 用于捕獲外部變量,parameters 是函數(shù)的參數(shù)列表,return-type 是函數(shù)的返回類型(可以省略,由編譯器自動推斷),function body 是函數(shù)體
以下是一個簡單的示例,演示如何使用lambda表達式來計算兩個整數(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表達式定義了一個函數(shù)sum,它接受兩個int類型的參數(shù)并返回它們的和。我們將該lambda表達式賦值給變量sum,并通過調用該變量來調用該函數(shù)。
capture list是lambda表達式中的一個可選項,它允許我們在函數(shù)體中捕獲外部變量。
外部變量:在函數(shù)體外部定義的變量,可以是全局變量,也可以是局部變量。(捕獲外部變量的主要原因是 lambda 表達式是一個匿名函數(shù),它沒有自己的名稱和作用域,因此無法像常規(guī)函數(shù)那樣直接訪問外部作用域中的變量。通過捕獲外部變量,lambda 表達式可以獲得對這些變量的訪問權,使其能夠在函數(shù)體中使用這些變量。)
捕獲可以按值或按引用進行,這決定了 lambda 表達式如何訪問外部變量。按值捕獲將外部變量的值復制到 lambda 表達式的閉包中,這意味著 lambda 表達式不會影響外部變量的值。按引用捕獲將外部變量的引用傳遞給 lambda 表達式,這意味著 lambda 表達式可以更改外部變量的值。
例如,我們可以使用以下lambda表達式來將一個整數(shù)加上一個固定的值:
#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來捕獲變量offset,并在lambda表達式中使用該變量。這樣,我們就可以使用該lambda表達式來將任何整數(shù)加上偏移量offset。
當然,你可以將變量作為參數(shù)直接傳遞給 lambda 表達式,但這種方式通常適用于較簡單的場景。在某些情況下,捕獲外部變量可以更方便地訪問外部作用域中的變量,并允許 lambda 表達式使用外部變量的狀態(tài)來執(zhí)行更復雜的操作。
以上是 algorithm 頭文件中最常用的函數(shù)及其使用方法,當然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),讀者可以通過查看 algorithm 頭文件的文檔來了解更多。
到此這篇關于C++常用函數(shù)的文章就介紹到這了,更多相關C++常用函數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法
這篇文章主要介紹了C++實現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法,是Windows應用程序設計中非常實用的方法,需要的朋友可以參考下2014-10-10實戰(zhàn)開發(fā)為單片機的按鍵加一個鎖防止多次觸發(fā)的細節(jié)
今天小編就為大家分享一篇關于實戰(zhàn)開發(fā)為單片機的按鍵加一個鎖防止多次觸發(fā)的細節(jié),小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12