for_each 와 boost bind

C++ 2012. 9. 7. 18:12




class Sample

{

public:

void test() {

vector<int> vec;

for (int i=0; i<10; ++i) {

vec.push_back(i);

}


string msg = "til i die";


for_each(vec.begin(), vec.end(), boost::bind(&Sample::oper, boost::ref(*this), _1, msg));

}

void oper(int i, const string& msg) {

cout << "1st(i) : " << i << ", 2nd(string) : " << msg << endl;

}

};


실행 : 

Sample sample;

sample.test();

결과 : 

1st(i) : 0, 2nd(string) : til i die

1st(i) : 1, 2nd(string) : til i die

1st(i) : 2, 2nd(string) : til i die

1st(i) : 3, 2nd(string) : til i die

1st(i) : 4, 2nd(string) : til i die

1st(i) : 5, 2nd(string) : til i die

1st(i) : 6, 2nd(string) : til i die

1st(i) : 7, 2nd(string) : til i die

1st(i) : 8, 2nd(string) : til i die

1st(i) : 9, 2nd(string) : til i die

'C++' 카테고리의 다른 글

윈도우 서비스  (0) 2012.10.11
process kill 프로세스 죽이기  (0) 2012.10.10
pid 구하기  (0) 2012.10.10
현재 프로그램 경로  (0) 2012.10.05
FireBreath  (0) 2012.08.10
c++ logging library  (0) 2012.08.08
std::regex - 대충 이런 느낌  (0) 2012.08.01
wxWidgets 컴파일  (0) 2012.07.24