|
이돈진 님이 쓰신 글 :
: 제목과 같습니다.
:
: STL 함수중에서 bind1st와 bind2nd의 차이점이 뭔가요?
두개의 파라메터중에
첫번째를 고정할지 두번째를 고정시킬지에 따라 선택하게 됩니다. ^^
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main(int argc, char *argv[])
{
vector<int> IntVec;
for(int i=0;i<100;++i) IntVec.push_back(i);
printf( "bind1st(greater<int>(),30) : %d\n",*find_if(IntVec.begin(),IntVec.end(),
bind1st(greater<int>(),30)));
printf( "bind2nd(greater<int>(),30) : %d\n",*find_if(IntVec.begin(),IntVec.end(),
bind2nd(greater<int>(),30)));
return 0;
}
결과 :
bind1st(greater<int>(),30) : 0
bind2nd(greater<int>(),30) : 31
bind1st 의 경우 30을 첫번째 파라메터에 고정시켰기 때문에
if ( 30 > *Iterator ) 로 검사를 하기 때문에 바로 0 에서 true로 리턴해 버리구요
bin2nd 의 경우 30을 두번째 파라메터에 고정시켰기 때문에
if ( *Iterator > 30 ) 로 검사를 하기 때문에 원하는 31에서 true로 리턴하게 됩니다.
아마 쉽게 이해가 되셨으리라고 생각합니다 ^^;
ps: 추가합니다. 소스가 오류가 있었습니다. -_- 1st,1nd , 2st,2nd 마구섞었군요. 급하게 쓰느라..
|