欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ accumulate函數(shù)詳細(xì)介紹和具體案例

 更新時(shí)間:2022年08月18日 16:48:59   作者:zhangbw~  
這篇文章主要介紹了C++ accumulate函數(shù)詳細(xì)介紹和具體案例,accumulate是numeric庫中的一個(gè)函數(shù),主要用來對指定范圍內(nèi)元素求和,但也自行指定一些其他操作,如范圍內(nèi)所有元素相乘、相除等

一、函數(shù)簡單介紹

accumulate是numeric庫中的一個(gè)函數(shù),主要用來對指定范圍內(nèi)元素求和,但也自行指定一些其他操作,如范圍內(nèi)所有元素相乘、相除等。

使用前需要引入相應(yīng)的頭文件。

#include <numeric>
  • 函數(shù)共有四個(gè)參數(shù),其中前三個(gè)為必須,第四個(gè)為非必需。
  • 若不指定第四個(gè)參數(shù),則默認(rèn)對范圍內(nèi)的元素進(jìn)行累加操作。
accumulate(起始迭代器, 結(jié)束迭代器, 初始值, 自定義操作函數(shù))

二、具體使用場景

1. 計(jì)算數(shù)組中所有元素的和

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
    vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate(arr.begin(), arr.end(), 0); // 初值0 + (1 + 2 + 3 + 4 +... + 10)
    cout << sum << endl;	// 輸出55
    return 0;
}

2. 計(jì)算數(shù)組中所有元素的乘積

需要指定第四個(gè)參數(shù),這里使用的是乘法函數(shù) multiplies<type>(), type根據(jù)元素的類型選擇。

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
    vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate(arr.begin(), arr.end(), 1, multiplies<int>()); // 初值1 * (1 * 2 * 3 * 4 *... * 10)
    cout << sum << endl;	// 輸出3628800
    return 0;
}

3. 計(jì)算數(shù)組中每個(gè)元素乘以3之后的和

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int fun(int acc, int num) {
    return acc + num * 3;     // 計(jì)算數(shù)組中每個(gè)元素乘以3
}

int main() {
    vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate(arr.begin(), arr.end(), 0, fun);
    cout << sum << endl;	// 輸出 165
    return 0;
}

4.計(jì)算數(shù)組中每個(gè)元素減去3之后的和

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int fun(int acc, int num) {
    return acc + (num - 3) ;     // 計(jì)算數(shù)組中每個(gè)元素減去3之后的和
}

int main() {
    vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate(arr.begin(), arr.end(), 0, fun);
    cout << sum << endl;    // 輸出25
    return 0;
}

5.計(jì)算班級(jí)內(nèi)學(xué)生的平均分

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

struct Student {
    string name;
    int score;
    Student() {};   // 無參構(gòu)造函數(shù)
    Student(string name, int score) : name(name), score(score) {};  // 有參構(gòu)造函數(shù)
};

int fun(int acc, Student b) {
    return a + b.score;
}

int main() {
    vector<Student> arr;
    arr.emplace_back("Alice", 82);
    arr.emplace_back("Bob", 91);
    arr.emplace_back("Lucy", 85);
    arr.emplace_back("Anna", 60);
    arr.emplace_back("June", 73);
    int avg_score = accumulate(arr.begin(), arr.end(), 0, fun) / arr.size();	// 總分/學(xué)生數(shù)
    cout << avg_score << endl;
    return 0;
}

6.拼接字符串

C++中字符串之間也可以使用+,即拼接兩個(gè)字符串。

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
    vector<string> words{"this ", "is ", "a ", "sentence!"};
    string init, res;
    res = accumulate(words.begin(), words.end(), init);    // 連接字符串
    cout << res << endl;    // this is a sentence!
    return 0;
}

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

相關(guān)文章

  • C++ Boost Spirit入門教程

    C++ Boost Spirit入門教程

    Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱
    2022-11-11
  • C++ 使用PrintWindow實(shí)現(xiàn)窗口截圖功能

    C++ 使用PrintWindow實(shí)現(xiàn)窗口截圖功能

    這篇文章主要介紹了C++ 如何使用PrintWindow實(shí)現(xiàn)窗口截圖功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • c/c++基礎(chǔ)簡單易懂的快速排序算法

    c/c++基礎(chǔ)簡單易懂的快速排序算法

    這篇文章主要為大家介紹了c/c++基礎(chǔ)非常簡單易懂的快速排序算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-11-11
  • C語言修煉之路函數(shù)篇真題訓(xùn)練下

    C語言修煉之路函數(shù)篇真題訓(xùn)練下

    函數(shù)是一組一起執(zhí)行一個(gè)任務(wù)的語句。每個(gè) C 程序都至少有一個(gè)函數(shù),即主函數(shù) main() ,所有簡單的程序都可以定義其他額外的函數(shù)
    2022-03-03
  • C++函數(shù)重載、隱藏與覆蓋重寫的精通指南

    C++函數(shù)重載、隱藏與覆蓋重寫的精通指南

    這篇文章主要給大家介紹了關(guān)于C++函數(shù)重載、隱藏與覆蓋重寫的相關(guān)資料,這幾個(gè)名詞看著好像很像,不過其實(shí)一樣都不一樣,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • 實(shí)例詳解C++中指針與引用的區(qū)別

    實(shí)例詳解C++中指針與引用的區(qū)別

    引用是C++引入的重要機(jī)制(C語言沒有引用),它使原來在C中必須用指針來實(shí)現(xiàn)的功能有了另一種實(shí)現(xiàn)的選擇,在書寫形式上更為簡潔,那么引用的本質(zhì)是什么,它與指針又有什么關(guān)系呢?這篇文章主要給大家介紹了關(guān)于C++中指針與引用的區(qū)別,需要的朋友可以參考下
    2021-07-07
  • QT實(shí)現(xiàn)簡單計(jì)算器功能

    QT實(shí)現(xiàn)簡單計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了QT實(shí)現(xiàn)簡單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Mingw64編譯wxWidgets 3.0.2常見錯(cuò)誤分析

    Mingw64編譯wxWidgets 3.0.2常見錯(cuò)誤分析

    這篇文章主要介紹了Mingw64編譯wxWidgets 3.0.2常見錯(cuò)誤分析,需要的朋友可以參考下
    2016-11-11
  • 最新評論