網站首頁 編程語言 正文
一、函數簡單介紹
accumulate是numeric
庫中的一個函數,主要用來對指定范圍內元素求和,但也自行指定一些其他操作,如范圍內所有元素相乘、相除等。
使用前需要引入相應的頭文件。
#include <numeric>
- 函數共有四個參數,其中前三個為必須,第四個為非必需。
- 若不指定第四個參數,則默認對范圍內的元素進行累加操作。
accumulate(起始迭代器, 結束迭代器, 初始值, 自定義操作函數)
二、具體使用場景
1. 計算數組中所有元素的和
#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. 計算數組中所有元素的乘積
需要指定第四個參數,這里使用的是乘法函數 multiplies<type>()
, type根據元素的類型選擇。
#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. 計算數組中每個元素乘以3之后的和
#include <iostream> #include <vector> #include <numeric> using namespace std; int fun(int acc, int num) { return acc + num * 3; // 計算數組中每個元素乘以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.計算數組中每個元素減去3之后的和
#include <iostream> #include <vector> #include <numeric> using namespace std; int fun(int acc, int num) { return acc + (num - 3) ; // 計算數組中每個元素減去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.計算班級內學生的平均分
#include <iostream> #include <vector> #include <numeric> using namespace std; struct Student { string name; int score; Student() {}; // 無參構造函數 Student(string name, int score) : name(name), score(score) {}; // 有參構造函數 }; 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(); // 總分/學生數 cout << avg_score << endl; return 0; }
6.拼接字符串
C++中字符串之間也可以使用
+
,即拼接兩個字符串。
#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; }
原文鏈接:https://zhangbw.blog.csdn.net/article/details/125257536
相關推薦
- 2023-01-15 盤點SqlServer?分頁方式和拉姆達表達式分頁_MsSql
- 2022-06-23 VBS?批量Ping的項目實現_vbs
- 2022-04-19 Python的閉包和裝飾器你真的了解嗎_python
- 2022-04-23 R語言兩組變量特征相關關系熱圖繪制畫法_R語言
- 2022-06-13 golang下grpc框架的使用編寫示例_Golang
- 2022-07-24 C#導入和導出CSV文件_C#教程
- 2022-10-05 C++淺析數據在內存中如何存儲_C 語言
- 2023-01-12 python可迭代類型遍歷過程中數據改變會不會報錯_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支