最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++常用函數(shù)總結(jié)(algorithm 頭文件)

 更新時間:2023年12月14日 11:42:19   作者:CX__CS  
本文給大家詳細(xì)介紹了algorithm 頭文件中最常用的函數(shù)及其使用方法,當(dāng)然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),感興趣的朋友一起看看吧

std::sort

std::sort 函數(shù)用于對數(shù)組或容器進(jìn)行排序,可以按照默認(rèn)的升序排序或指定比較函數(shù)進(jìn)行排序。

語法如下:

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ù)對象。

示例代碼如下:

#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ù)組進(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ù)用于獲取兩個值中的最大值或最小值。

語法如下:

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;
}

輸出結(jié)果為:

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ū)間的終止地址,不包含在搜索范圍內(nèi)。
  • 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;
}

輸出結(jié)果為:

Found

std::count

std::count 函數(shù)用于統(tǒng)計(jì)容器或數(shù)組中指定值的個數(shù)。

語法如下:

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 的元素個數(shù)
    std::cout << "count = " << cnt << std::endl;
    return 0;
}

輸出結(jié)果為:

count = 3

std::accumulate

std::accumulate 函數(shù)用于計(jì)算容器或數(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ū)間的終止地址,不包含在求和范圍內(nèi)。
  • init:可選參數(shù),求和的初始值。
  • binary_op:可選參數(shù),二元運(yùn)算函數(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;
}

輸出結(jié)果為:

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ū)間的終止地址,不包含在遍歷范圍內(nèi)。
  • 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;
}

輸出結(jié)果為:

1 2 3 4 5

std::unique

std::unique 函數(shù)用于去除容器或數(shù)組中的重復(fù)元素,僅保留第一個出現(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ū)間的終止地址,不包含在去重范圍內(nèi)。
  • 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()); // 去除容器中的重復(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)。

語法如下:

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ū)間的元素賦值為指定的值。

語法如下:

template <class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& value);
  • first:要填充的第一個元素的迭代器。
  • last:要填充的最后一個元素的迭代器,不包括在范圍內(nèi)。
  • 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;
}

輸出結(jié)果為:

3 3 3 3 3 3 3 3 3 3

另外需要注意的是,如果使用 fill 函數(shù)填充的元素是自定義類型,需要自行定義該類型的賦值運(yùn)算符重載函數(shù)。

std::next_permutation

std::next_permutation 函數(shù)用于求出容器或數(shù)組中元素的下一個排列組合。

std::next_permutation 是`cpp 標(biāo)準(zhǔn)庫中的一個函數(shù),用于返回一組元素的下一個排列。下面是該函數(shù)的定義:

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last);

該函數(shù)接受兩個迭代器作為參數(shù),表示需要進(jìn)行排列的元素范圍。

函數(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 進(jìn)行了排列,并使用了一個 do-while 循環(huán)來重復(fù)輸出排列結(jié)果,直到排列到了最后一個排列為止。

std::partition

std::partition 函數(shù)用于將容器或數(shù)組中的元素按照指定的條件進(jìn)行分區(qū),使得滿足條件的元素排在不滿足條件的元素之前。

std::partition是一個標(biāo)準(zhǔn)庫函數(shù),可以將一組元素重新排序,使得滿足某個特定條件的元素在序列的前面,而不滿足條件的元素在后面。該函數(shù)接受三個參數(shù):

template <typename ForwardIt, typename UnaryPredicate>
ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p);

第一個參數(shù)是指向第一個元素的迭代器,第二個參數(shù)是指向最后一個元素后面的迭代器(即last不在范圍內(nèi)),第三個參數(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表達(dá)式is_odd作為一元謂詞,該謂詞返回true表示元素是奇數(shù),返回false表示元素是偶數(shù)。函數(shù)將一個包含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指向序列中最后一個奇數(shù)的后面一個位置。函數(shù)std::copy將奇數(shù)和偶數(shù)分別打印出來。

注:lambda表達(dá)式`cpp11中新增的特性:

`cpp11中,可以使用lambda表達(dá)式來定義一個匿名函數(shù),語法格式如下:

[capture list] (parameters) -> return-type { function body }

其中,capture list 用于捕獲外部變量,parameters 是函數(shù)的參數(shù)列表,return-type 是函數(shù)的返回類型(可以省略,由編譯器自動推斷),function body 是函數(shù)體

以下是一個簡單的示例,演示如何使用lambda表達(dá)式來計(jì)算兩個整數(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á)式定義了一個函數(shù)sum,它接受兩個int類型的參數(shù)并返回它們的和。我們將該lambda表達(dá)式賦值給變量sum,并通過調(diào)用該變量來調(diào)用該函數(shù)。

capture list是lambda表達(dá)式中的一個可選項(xiàng),它允許我們在函數(shù)體中捕獲外部變量。

外部變量:在函數(shù)體外部定義的變量,可以是全局變量,也可以是局部變量。(捕獲外部變量的主要原因是 lambda 表達(dá)式是一個匿名函數(shù),它沒有自己的名稱和作用域,因此無法像常規(guī)函數(shù)那樣直接訪問外部作用域中的變量。通過捕獲外部變量,lambda 表達(dá)式可以獲得對這些變量的訪問權(quán),使其能夠在函數(shù)體中使用這些變量。)

捕獲可以按值或按引用進(jìn)行,這決定了 lambda 表達(dá)式如何訪問外部變量。按值捕獲將外部變量的值復(fù)制到 lambda 表達(dá)式的閉包中,這意味著 lambda 表達(dá)式不會影響外部變量的值。按引用捕獲將外部變量的引用傳遞給 lambda 表達(dá)式,這意味著 lambda 表達(dá)式可以更改外部變量的值。

例如,我們可以使用以下lambda表達(dá)式來將一個整數(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表達(dá)式中使用該變量。這樣,我們就可以使用該lambda表達(dá)式來將任何整數(shù)加上偏移量offset。

當(dāng)然,你可以將變量作為參數(shù)直接傳遞給 lambda 表達(dá)式,但這種方式通常適用于較簡單的場景。在某些情況下,捕獲外部變量可以更方便地訪問外部作用域中的變量,并允許 lambda 表達(dá)式使用外部變量的狀態(tài)來執(zhí)行更復(fù)雜的操作。

以上是 algorithm 頭文件中最常用的函數(shù)及其使用方法,當(dāng)然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),讀者可以通過查看 algorithm 頭文件的文檔來了解更多。

到此這篇關(guān)于C++常用函數(shù)的文章就介紹到這了,更多相關(guān)C++常用函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++超詳細(xì)講解RTTI和cast運(yùn)算符的使用

    C++超詳細(xì)講解RTTI和cast運(yùn)算符的使用

    RTTI(Runtime Type Identification)是“運(yùn)行時類型識別”的意思。C++引入這個機(jī)制是為了讓程序在運(yùn)行時能根據(jù)基類的指針或引用來獲得該指針或引用所指的對象的實(shí)際類型,cast強(qiáng)制轉(zhuǎn)換運(yùn)算符是一種特殊的運(yùn)算符,它把一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)類型
    2022-08-08
  • Matlab實(shí)現(xiàn)繪制立體玫瑰花的示例代碼

    Matlab實(shí)現(xiàn)繪制立體玫瑰花的示例代碼

    這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)繪制更立體的玫瑰花,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下
    2023-02-02
  • C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐

    C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐

    本文主要介紹了C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐,根據(jù)節(jié)點(diǎn)插入位置的不同,AVL樹的旋轉(zhuǎn)分為四種,下面就來介紹一下,感興趣的可以了解一下
    2024-03-03
  • 教你用Matlab制作立體動態(tài)相冊

    教你用Matlab制作立體動態(tài)相冊

    沒想到吧,MATLAB竟也能制作3D相冊!本文將為大家詳細(xì)介紹Matlab制作立體動態(tài)相冊的方法步驟,感興趣的小伙伴可以跟隨小編一起動手試一試
    2022-03-03
  • 利用Matlab制作一款狗頭翻牌子小游戲

    利用Matlab制作一款狗頭翻牌子小游戲

    本文將用Matlab制作一個狗頭翻牌子的小游戲,就是點(diǎn)擊一個牌子時,該牌子和周圍四個牌子也會相應(yīng)發(fā)生變化,想辦法讓所有牌子都在同一面即為游戲勝利。感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-03-03
  • 貪吃蛇游戲C++命令行版實(shí)例代碼

    貪吃蛇游戲C++命令行版實(shí)例代碼

    這篇文章主要介紹了貪吃蛇游戲C++命令行版實(shí)例代碼,包含了常見的循環(huán)語句及相關(guān)游戲規(guī)則的判定方法,有助于更好的理解游戲設(shè)計(jì)原理,需要的朋友可以參考下
    2014-09-09
  • VisualStudio2010安裝教程

    VisualStudio2010安裝教程

    這篇文章通過圖文并茂的形式給大家介紹VisualStudio2010安裝教程,在日常開發(fā)中是必不可少的搭建過程,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • C語言實(shí)現(xiàn)簡單登錄操作

    C語言實(shí)現(xiàn)簡單登錄操作

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單登錄操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C/C++?Linux?Socket網(wǎng)絡(luò)編程流程分析

    C/C++?Linux?Socket網(wǎng)絡(luò)編程流程分析

    這篇文章主要介紹了C/C++?Linux?Socket網(wǎng)絡(luò)編程,Linux環(huán)境中的C/C++?socket?與Window環(huán)境中的C/C++?socket類似,本文所記錄的是TCP協(xié)議的socket編程,圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • c語言顏色代碼詳解

    c語言顏色代碼詳解

    在本篇文章里小編給大家整理的是關(guān)于c語言顏色代碼的知識點(diǎn)內(nèi)容,需要的朋友們可以參考下。
    2020-02-02

最新評論

青海省| 疏附县| 综艺| 东明县| 纳雍县| 平阴县| 白城市| 万盛区| 武强县| 萨嘎县| 招远市| 浮梁县| 永修县| 土默特右旗| 郯城县| 于都县| 合作市| 横山县| 仁寿县| 山西省| 抚州市| 梧州市| 本溪| 诸城市| 广水市| 冀州市| 新安县| 喀什市| 罗山县| 五大连池市| 井冈山市| 宝坻区| 京山县| 许昌县| 秦皇岛市| 高台县| 敦化市| 绥棱县| 西宁市| 新化县| 广安市|