바인더, 부정자

seio·2022년 10월 9일
0

C++ STL

목록 보기
16/17

바인더

바인더(binder)는 이항 함수자를 단항 함수자로 변환합니다. STL은 다음 두 가지 바인더를 제공한다.

  • bind1st: 이항 함수자의 첫 번째 인자를 고정하여 단항 함수자로 변환
  • bind2nd: 이항 함수자의 두 번째 인자를 고정하여 단항 함수자로 변환

//bind1st

	int main()
    {
    	binder1st<less<int>> binder = bind1st(less<int>(),10);
        
        cout<<binder(5)<<"="<<less<int>(10,5)<<endl;
        cout<<binder(10)<<"="<<less<int>(10,10)<<endl;
        cout<<binder(20)<<"="<<less<int>(10,20)<<endl;
        
        //임시객체 사용
        cout<<bind1st(less<int>(),10)(5)<<"="<<less<int>(10,5)<<endl;
        cout<<bind1st(less<int>(),10)(10)<<"="<<less<int>(10,10)<<endl;
        cout<<bind1st(less<int>(),10)(20)<<"="<<less<int>(10,20)<<endl; 
    }

//bind2nd

	int main()
    {
    	binder2nd<less<int>> binder = bind2nd(less<int>(),10);
        
        cout<<binder(5)<<"="<<less<int>(5,10)<<endl;
        cout<<binder(10)<<"="<<less<int>(10,10)<<endl;
        cout<<binder(20)<<"="<<less<int>(20,10)<<endl;
        
        //임시객체 사용
        cout<<bind2nd(less<int>(),10)(5)<<"="<<less<int>(5,10)<<endl;
        cout<<bind2nd(less<int>(),10)(10)<<"="<<less<int>(10,10)<<endl;
        cout<<bind2nd(less<int>(),10)(20)<<"="<<less<int>(20,10)<<endl; 
    }

부정자

부정자(negator)는 조건자를 반대의 조건자로 변환한다. STL은 두 가지 부정자를 제공한다.

  • not1: 단항 조건자를 반대의 조건자로 변환
  • not2: 이항 조건자를 반대의 조건자로 변환

//not2 부정자

	int main(){
    	less<int> oLess;
        binary_negate<less<int>> negate = not2(less<int>());
        
        cout<<negate(5,10)<<"="<<not2(oLess)(5,10)<<"="<<not2(less<int>())(5,10)<<endl;
        
        cout<<negate(10,10)<<"="<<not2(oLess)(10,10)<<"="<<not2(less<int>())(10,10)<<endl;
                
        cout<<negate(20,10)<<"="<<not2(oLess)(20,10)<<"="<<not2(less<int>())(20,10)<<endl;
    }
    [출력 결과]
    0=0=0
    1=1=1
    1=1=1

// 이항 조건자를 바인더로 단항 조건자를 만들고 다시 not1 부정자로 반대의 단항 조건자로 변환하는 예제

	int main(){
    	binder2nd<less<int>> binder = bind2nd(less<int>(),10);
        unary_negate<binder2nd<less<int>>> negate = not1(binder);
        
        cout<<negate(5)<<"="<<not2(binder)(5)<<"="<<not1(bind2nd(less<int>(),10))(5)<<endl;
        
        cout<<negate(10)<<"="<<not2(binder)(10)<<"="<<not1(bind2nd(less<int>(),10))(10)<<endl;
                
        cout<<negate(20)<<"="<<not2(binder)(20)<<"="<<not1(bind2nd(less<int>(),10))(20)<<endl;
    }
    [출력 결과]
    0=0=0
    1=1=1
    1=1=1
profile
personal study area

0개의 댓글