C++ STL 数值算法 对数列进行某种运算 accumulate使用方法

在这里插入图片描述

template<typename T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
	for (int i = first; i <= last; ++i)
	{
		coll.insert(coll.end(), i);
	}
}
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
	cout << optcstr;
	for (auto elem : coll)
	{
		cout << elem << ' ';
	}
	cout << endl;
}
int main()
{
	list<int>a;
	INSERT_ELEMENTS(a, 1, 9);
	PRINT_ELEMENTS(a, "a: ");
	cout << "sum: " << accumulate(a.begin(), a.end(), 0) << endl;
	cout << "product: " << accumulate(a.cbegin(), a.cend(), 1, multiplies<int>()) << endl;
	cout << "product: " << accumulate(a.cbegin(), a.cend(), 0, multiplies<int>()) << endl;

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44800780/article/details/104194820