一、元素累加算法 - accumulate 函數(shù)
1、函數(shù)原型分析
在 C++ 語(yǔ)言 的 標(biāo)準(zhǔn)模板庫(kù) ( STL , STL Standard Template Library ) 中 , 提供了 accumulate 元素累加算法函數(shù) 用于 將 一個(gè)容器中的元素 進(jìn)行累加操作 ;
accumulate 元素累加函數(shù) 將 輸入容器 的 [ 起始迭代器, 終止迭代器 ) 范圍 內(nèi)的 元素 在一個(gè)基礎(chǔ)值 的 基礎(chǔ)上 進(jìn)行累加 , 得到一個(gè)累加值 ;
最終 accumulate 函數(shù) 返回最終累加后的值 ;
accumulate 元素累加算法 函數(shù)原型 如下 :
template <class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
-
參數(shù)解析 :
- InputIterator first 參數(shù) : 輸入容器 ( 被復(fù)制容器 ) 的 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- InputIterator last 參數(shù) : 輸入容器 ( 被復(fù)制容器 ) 的 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- T init 參數(shù) : 累加的初始值 , 該值與 容器中的元素類(lèi)型一致 ;
- 返回值解析 : T 類(lèi)型 是 容器元素類(lèi)型 , 返回的是最終的累加值 ;
代碼示例 :
// 輸入容器
vector<int> source{ 9, 5, 2, 7 };
// 將容器中的值累加
int acc = accumulate(source.begin(), source.end(), 0);
2、代碼示例
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
// accumulate 函數(shù)定義在這個(gè)頭文件中
#include <numeric>
int main() {
// 輸入容器
vector<int> source{ 9, 5, 2, 7 };
// 將容器中的值累加
int acc = accumulate(source.begin(), source.end(), 0);
// 遍歷打印容器中元素內(nèi)容
for_each(source.begin(), source.end(), [](int a) {
cout << a << " ";
});
cout << endl;
cout << "acc = " << acc << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9 5 2 7
acc = 23
請(qǐng)按任意鍵繼續(xù). . .
二、元素填充算法 - fill 函數(shù)
1、函數(shù)原型分析
在 C++ 語(yǔ)言 的 標(biāo)準(zhǔn)模板庫(kù) ( STL , STL Standard Template Library ) 中 , 提供了 fill 元素填充算法函數(shù) 用于 將 一個(gè)容器中的 指定范圍的元素 修改為指定值 ;
fill 元素填充函數(shù) 將 輸入容器 的 [ 起始迭代器, 終止迭代器 ) 范圍 內(nèi)的 元素 修改為指定值 ;
fill 元素填充算法 函數(shù)原型 如下 :
template <class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& value);
-
參數(shù)解析 :
- ForwardIterator first 參數(shù) : 輸入容器 ( 被復(fù)制容器 ) 的 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- ForwardIterator last 參數(shù) : 輸入容器 ( 被復(fù)制容器 ) 的 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- const T& value 參數(shù) : 要求改的值
- 返回值解析 : void 類(lèi)型返回值 ;
代碼示例 :
// 輸入容器
vector<int> source{ 9, 5, 2, 7 };
// 將容器中的值都填充為 888
fill(source.begin(), source.end(), 888);
2、代碼示例
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
// accumulate 函數(shù)定義在這個(gè)頭文件中
#include <numeric>
int main() {
// 輸入容器
vector<int> source{ 9, 5, 2, 7 };
// 遍歷打印容器中元素內(nèi)容
for_each(source.begin(), source.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 將容器中的值都填充為 888
fill(source.begin(), source.end(), 888);
// 遍歷打印容器中元素內(nèi)容
for_each(source.begin(), source.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9 5 2 7
888 888 888 888
請(qǐng)按任意鍵繼續(xù). . .文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-813442.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-813442.html
到了這里,關(guān)于【C++】STL 算法 - 累加填充算法 ( 元素累加算法 - accumulate 函數(shù) | 元素填充算法 - fill 函數(shù) )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!