C++ numeric庫簡介與使用指南
好的,我會幫你詳細(xì)介紹 C++ <numeric> 庫 及其常用函數(shù),并附帶示例代碼,方便理解。
以下內(nèi)容會涵蓋 <numeric> 的主要功能、常用函數(shù)、適用場景以及注意事項。
1.<numeric>庫簡介
<numeric> 是 C++ 標(biāo)準(zhǔn)庫中的一個頭文件,主要提供了一些 數(shù)值運算工具函數(shù),用來對序列(通常是容器中的元素)進(jìn)行累加、相鄰差值計算、內(nèi)積、序列生成等操作。
常用特性:
- 工作對象:任何支持迭代器(尤其是輸入迭代器)的序列,例如
std::vector、std::array、原生數(shù)組等。 - 泛型:可以作用于任意類型(只要它們支持相應(yīng)的運算符,比如
+、-、*等)。
2. 常用函數(shù)總覽
| 函數(shù) | 作用 | 頭文件 |
|---|---|---|
std::accumulate | 對范圍內(nèi)的元素進(jìn)行累加或自定義的二元操作 | <numeric> |
std::reduce (C++17) | 類似 accumulate,但可并行實現(xiàn)(需 <execution> 支持) | <numeric> |
std::inner_product | 計算兩個序列的內(nèi)積(可自定義加法和乘法運算) | <numeric> |
std::adjacent_difference | 計算相鄰元素的差值(或自定義二元操作) | <numeric> |
std::partial_sum | 計算部分和(或自定義二元操作) | <numeric> |
std::inclusive_scan / exclusive_scan (C++17) | 類似 partial_sum,但有更多控制和并行支持 | <numeric> |
std::iota | 從一個初始值開始,為范圍內(nèi)的元素按遞增填充值 | <numeric> |
3. 常用函數(shù)詳解
3.1std::accumulate
功能: 對一個迭代器范圍內(nèi)的元素求和,或用自定義函數(shù)進(jìn)行累積。
語法:
template<class InputIt, class T> T accumulate(InputIt first, InputIt last, T init); template<class InputIt, class T, class BinaryOperation> T accumulate(InputIt first, InputIt last, T init, BinaryOperation op);
示例:
#include <iostream>
#include <vector>
#include <numeric> // accumulate
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0);
std::cout << "Sum: " << sum << "\n";
// 計算乘積
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<>());
std::cout << "Product: " << product << "\n";
}輸出:
Sum: 15
Product: 120
3.2std::inner_product
功能: 計算兩個序列的內(nèi)積,可以自定義加法和乘法運算。
語法:
template<class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init);
template<class InputIt1, class InputIt2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOperation1 op1, BinaryOperation2 op2);示例:
#include <iostream>
#include <vector>
#include <numeric> // inner_product
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
int result = std::inner_product(a.begin(), a.end(), b.begin(), 0);
std::cout << "Inner product: " << result << "\n"; // 32
// 使用自定義操作(這里是 a[i]-b[i] 的和)
int diff_sum = std::inner_product(a.begin(), a.end(), b.begin(), 0,
std::plus<>{}, std::minus<>{});
std::cout << "Sum of differences: " << diff_sum << "\n"; // -9
}3.3std::adjacent_difference
功能: 生成一個新序列,每個元素為輸入序列中相鄰兩個元素的差值(可自定義二元運算)。
語法:
template<class InputIt, class OutputIt> OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first); template<class InputIt, class OutputIt, class BinaryOperation> OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op);
示例:
#include <iostream>
#include <vector>
#include <numeric> // adjacent_difference
int main() {
std::vector<int> v = {1, 3, 6, 10, 15};
std::vector<int> result(v.size());
std::adjacent_difference(v.begin(), v.end(), result.begin());
for (int n : result) std::cout << n << " "; // 1 2 3 4 5
}3.4std::partial_sum
功能: 生成部分和序列(可自定義二元運算)。
語法:
template<class InputIt, class OutputIt> OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first); template<class InputIt, class OutputIt, class BinaryOperation> OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op);
示例:
#include <iostream>
#include <vector>
#include <numeric> // partial_sum
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());
std::partial_sum(v.begin(), v.end(), result.begin());
for (int n : result) std::cout << n << " "; // 1 3 6 10 15
}3.5std::iota
功能: 填充一個范圍,從某個初始值開始遞增。
語法:
template<class ForwardIt, class T> void iota(ForwardIt first, ForwardIt last, T value);
示例:
#include <iostream>
#include <vector>
#include <numeric> // iota
int main() {
std::vector<int> v(5);
std::iota(v.begin(), v.end(), 10); // 從 10 開始
for (int n : v) std::cout << n << " "; // 10 11 12 13 14
}4. 使用建議與注意事項
- 當(dāng)使用浮點數(shù)時,累加運算可能會有 精度誤差(特別是大規(guī)模數(shù)據(jù))。
- 對于需要高性能的并行數(shù)值計算,可以使用
std::reduce或std::transform_reduce(C++17+)。 - 這些算法可以搭配 標(biāo)準(zhǔn)函數(shù)對象(如
std::plus<>、std::multiplies<>)或 lambda函數(shù),靈活性很高。 <numeric>函數(shù)不會修改原序列,除非明確要求將結(jié)果輸出到同一位置(如partial_sum)。
到此這篇關(guān)于C++ numeric庫簡介與使用指南的文章就介紹到這了,更多相關(guān)C++ numeric庫使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度】
這篇文章主要介紹了C語言二叉樹常見操作,結(jié)合實例形式詳細(xì)分析了基于C語言的二叉樹前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-04-04
C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用介紹
這篇文章主要介紹了C/C++中數(shù)據(jù)類型轉(zhuǎn)換詳解及其作用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
C++實現(xiàn)LeetCode(137.單獨的數(shù)字之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(137.單獨的數(shù)字之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言編程中對目錄進(jìn)行基本的打開關(guān)閉和讀取操作詳解
這篇文章主要介紹了C語言編程中對目錄進(jìn)行基本的打開關(guān)閉和讀取操作,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
QT中start()和startTimer()的區(qū)別小結(jié)
QTimer提供了定時器信號和單觸發(fā)定時器,本文主要介紹了QT中start()和startTimer()的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09

