7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

REPマクロをクラス化してみた

Last updated at Posted at 2015-04-15

REPマクロとは

競技プログラミングで使われるらしい?

#define REP(i,n) for (int i=0;i<(n);i++)

こんなマクロを定義しておいて

REP(i, 5) {
	REP(j, 10) {
		std::cout << i << ", " << j << std::endl;
	}
}

みたいに使うっぽい
はぇ~って感じだ(小並感)

クラス化してみる

(何となく試してみただけで実用性は)ないです


class repeat {
public:
	// コンストラクタでループする
	repeat(size_t count, std::function<void()> lambda) {
		size_t index = get().size();
		// カウンタを追加
		get().push_back(0);
		// count 回 lambda を実行
		for (; get()[index] < count; get()[index]++) {
			lambda();
		}
		// カウンタを削除
		get().pop_back();
	}
	// ループ回数を取得する
	static size_t count(size_t index) {
		return (index >= get().size()) ? 0 : get()[index];
	}
	// get()[index] = count(index)
	static std::vector<size_t> &get() {
		static std::vector<size_t> count;
		return count;
	}

};

このクラスを使って


repeat(5, [] {
	repeat(10, [] {
		std::cout << repeat::count(0) << ", " << repeat::count(1) << std::endl;
	});
});

みたいに使う

結論

短く書かなくていいならふつうに for した方がいい(震え声)

7
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?