ジェネリックプログラミングのC ++開発講義18 [一般的に使用される算術生成アルゴリズム]

1つ、蓄積する

機能の説明:

  • 間隔の内容の累積合計を計算します

関数プロトタイプ:

  • accumulate(iterator beg, iterator end, value);

    //コンテナ要素の累積合計を計算します

    // begがイテレータを開始します

    //終了はイテレータを終了します

    //値の開始値

例:

#include <numeric>
#include <vector>
void test01()
{
	vector<int> v;
	for (int i = 0; i <= 100; i++) {
		v.push_back(i);
	}

	int total = accumulate(v.begin(), v.end(), 0);

	cout << "total = " << total << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

概要:accumulateを使用する場合、ヘッダーファイルは数値である必要があります。このアルゴリズムは非常に実用的です。

二、埋める

機能の説明:

  • 指定された要素でコンテナを埋めます

関数プロトタイプ:

  • fill(iterator beg, iterator end, value);

    //コンテナを要素で埋めます

    // begがイテレータを開始します

    //終了はイテレータを終了します

    //値が入力されます

例:

#include <numeric>
#include <vector>
#include <algorithm>

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{

	vector<int> v;
	v.resize(10);
	//填充
	fill(v.begin(), v.end(), 100);

	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

概要:fillを使用して、コンテナー間隔の要素を指定された値まで塗りつぶします

 

おすすめ

転載: blog.csdn.net/Kukeoo/article/details/114108560