Generator in C++

Tags:

http://www.crystalclearsoftware.com/soc/coroutine/index.html#coroutine.intro
http://www.sgi.com/tech/stl/generate.html

이렇게 할 수 있다는 건 미처 생각 못해봤네요…

#include

using namespace std;

class Generator {
private:
int min;
int max;
int val;

public:
Generator(int minInit, int maxInit):min(minInit), max(maxInit) {
val = minInit;
}

int operator()() {
return val++;
}

operator bool() const {
return val < max; } }; int main() { Generator g(10, 20); while (g) { cout << g() << endl; } return EXIT_SUCCESS; } [/code] Google Summer of Code에서 만들어졌지만 boost 안으로 들어오지는 못한 라이브러리인듯. Iterator로 만드는건 다음에 옮겨오도록 하죠. 원본의 iterator 예제가 마음에 안들어서...

Comments

2 responses to “Generator in C++”

  1. 홍민희 Avatar

    제가 Python의 제너레이터만 알아서 그런지… 이건 제너레이터라기 보다는 range에 가까운 것 같습니다.

  2. mkseo Avatar
    mkseo

    아 네… 본래 코드는 range generator라는 클래스명이었어요. yield를 만든코드가 있는데, 나중에 기회 닿으면 올려볼께요.

Leave a Reply

Your email address will not be published. Required fields are marked *