반응형

BLOG main image




#include <boost/bind.hpp>

#include <boost/mem_fn.hpp>



class C{

public :

C(){

c=0;

}


int c;

};


class A{

public :

A()

{

a=0;

}

int a;

void show( C& ci ){

std::cout<< a++ << "\t"<< ci.c <<std::endl;

++ci.c;


}


};


int main()

{



std::vector<A> ai;

ai.resize(10);


C ci;

std::for_each(ai.begin(),ai.end(), boost::bind( boost::mem_fn( &A::show )  , _1 ,boost::ref(ci) ) );

        //이부분에서 boost::ref(를 제거하면 다음 for_each 에서 0부터 출력하게된다 , ref가 없다면 for_each로 넘길대

       // 복사로 넘어가게 됨으로


std::cout<<std::endl;


std::for_each(ai.begin(),ai.end(), boost::bind( boost::mem_fn( &A::show )  , _1 ,ci ) );



return 0;


}


위 소스 그대로 했을때의 출력결과


0       0

0       1

0       2

0       3

0       4

0       5

0       6

0       7

0       8

0       9


1       10

1       11

1       12

1       13

1       14

1       15

1       16

1       17

1       18

1       19






ref를 제거했을대의 결과



0       0

0       1

0       2

0       3

0       4

0       5

0       6

0       7

0       8

0       9


1       0

1       1

1       2

1       3

1       4

1       5

1       6

1       7

1       8

1       9




반응형

'메타프로그래밍 > Boost::' 카테고리의 다른 글

boost::dynamic_bitset  (0) 2013.03.08
boost::boost::unordered_map, unordered_set  (0) 2013.03.02
boost::bind & ref, cref  (0) 2013.02.28
boost::circular_buffer  (0) 2013.02.26
boost::any  (0) 2013.02.13

+ Recent posts