#include <iostream>
#include <vector>
#include <algorithm> // for_each, generate に必要
#include <numeric> // transform に必要
#include <functional> // bind1st に必要
using namespace std;
typedef int Type; // なんでもいいけど取り扱う型
inline
const Type gen_func(void) // コンストラクター用関数
{
static Type x(0);
return x++;
}
class Aclass // クラス Aclass のインターフェイス
{
protected:
vector<Type> v;
public:
Aclass(const int size); // コンストラクター
void Calc(void); // 「計算しろ」
void Output(void); // 「v を出力しろ」
const Type Func(const Type x) { return x * x; } // これで transform
};
inline
Aclass::Aclass(const int size) : // コンストラクター
v(vector<Type>(size))
{
generate(v.begin(), v.end(), gen_func);
}
inline
void Aclass::Calc(void) // v のそれぞれの要素を Aclass::Func で変換する
{
transform(
v.begin(), v.end(), v.begin(),
bind1st(mem_fun(&Aclass::Func), this) // !!! STL の呪い !!!
);
}
inline
void output(const Type& x) // for_each で出力させるための関数
{
cout << " " << x;
}
inline
void Aclass::Output(void)
{
for_each(v.begin(), v.end(), output);
cout << endl;
}
// -----------------------------------------------------------------------------
int main (void)
{
Aclass a_class(10);
a_class.Output();
a_class.Calc();
a_class.Output();
return 0;
}
// -----------------------------------------------------------------------------
// 実行すると
//
// 0 1 2 3 4 5 6 7 8 9
// 0 1 4 9 16 25 36 49 64 81
//
// というような結果出力が得られる
// -----------------------------------------------------------------------------