1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /* generate 알고리즘은 구간 [first, last)에 해당하는 부분을, 함수 gen(세 번째 인자로 넘겨받은 함수)을 last-first 번 연속 호출하여 얻은 값들로 채운다. 여기서 gen 함수는 인자가 없는 함수라고 가정한다. generate 알고리즘의 시간 복잡도는 선형적이다. */ #include <iostream> #include <cassert> #include <algorithm> #include <vector> using namespace std; template < typename T> class calc_square { T i; public : calc_square() : i(0) {} T operator() () { ++i; return i*i; } }; int main() { cout<< "Illustrating the generic generate algorithm." << endl; vector< int > vector1(10); // vector1을 1, 4, 9, 16, ..., 100 으로 채운다. generate(vector1.begin(), vector1.end(), calc_square< int >()); for ( int j=0; j< 10; ++j) assert (vector1[j] == (j+1)*(j+1)); cout<< " --- Ok." << endl; return 0; } |
'STL > 변경 가능 시퀀스 알고리즘' 카테고리의 다른 글
[STL] remove (0) | 2010.05.17 |
---|---|
[STL] random_shuffle (0) | 2010.05.17 |
[STL] partition (0) | 2010.05.17 |
[STL] fill (0) | 2010.05.17 |
[STL] copy (0) | 2010.05.17 |