国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【C++】STL 算法 - 累加填充算法 ( 元素累加算法 - accumulate 函數(shù) | 元素填充算法 - fill 函數(shù) )

這篇具有很好參考價(jià)值的文章主要介紹了【C++】STL 算法 - 累加填充算法 ( 元素累加算法 - accumulate 函數(shù) | 元素填充算法 - fill 函數(shù) )。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。





一、元素累加算法 - 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ù). . .

【C++】STL 算法 - 累加填充算法 ( 元素累加算法 - accumulate 函數(shù) | 元素填充算法 - fill 函數(shù) ),C++,c++,算法,開(kāi)發(fā)語(yǔ)言,stl,STL算法,accumulate,fill





二、元素填充算法 - 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ù). . .

【C++】STL 算法 - 累加填充算法 ( 元素累加算法 - accumulate 函數(shù) | 元素填充算法 - fill 函數(shù) ),C++,c++,算法,開(kāi)發(fā)語(yǔ)言,stl,STL算法,accumulate,fill文章來(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包