C++STL中的求和函数accumulate()

C++STL中的求和函数accumulate()

1.1 函数原型及描述

accumulate(_InIt _First, _InIt _Last, _Ty _Val)

_First_Last累加的区间,_Val累加的初值。
返回类型跟_Val一致。

1.2 int中应用

输出数组中的和

	vector<int> testArray = { 1, 2, 3, 4 };
	int sumT = accumulate(testArray.begin(), testArray.end(), 0);

1.2 string中应用

将char类型拼接为string类型

	vector<char> testChr = { 'l', 'h', 'k' };
	string strSum = accumulate(testChr.begin(), testChr.end(), (string)" ");	\\sunT = "lhk"
发布了13 篇原创文章 · 获赞 1 · 访问量 297

猜你喜欢

转载自blog.csdn.net/qq_38670588/article/details/104455535