반응형

cpp functional

#include <functional>


++ operator class


++ 파라미터 바인더

+bind1st ; 함수객체를 리턴한다. 첫번째 파라미터를 지정.

ex) 10을 값으로 갖는 요소의 개수.

cnt = count_if( numbers, numbers+6,  bind1st( equal_to<int>(), 10 ) ) ;


+bind2nd ; 함수 객체를 리턴한다. 두번째 파라미터를 지정.

ex) 0보다 작은 값의 요소 개수.

cnt = count_if( numbers, numbers+6, bind2nd( less<int>(), 0 ) ) ;


++ 연산

+plus ; 두 객체를 더함.

template <class T> struct plus : binary_function <T,T,T> {

T operator() (const T&x, const T&y) const { return x+y ; }

ex) 두 컨테이너를 순서대로 요소별로 더하기

int first[]={1,2,3,4,5} ;

int second[]={10,20,30,40,50} ;

int results[5] ;

std::transform( first, first+5, second, results, std::plus<int>()) ;


+minus: 두 객체를 뺌.

상동

ex)

int numbers[]={10,20,30} ;

int result ;

result = std::accumulate( numbers, numbers+3, 100, std::minus<int>() ) ;

// 100 -10-20-30 = 40


+multiplies ; 두 객체를 더함.

int numbers[]={1,2,3,4,5,6,7,8,9} ;

int factorials[9] ;    // 각각의 factorial을 구함.

std::partial_sum( numbers, numbers+9, factorials, std::multiplies<int>() ) ;

// 시작값=1, 두개의 곲=2, 다음 곱=6, ...

//factorials =  1! 2! 3! 4! 5! 6! 7! 8! 9! 


+divides ; 두 객체를 나눔. first/second

// first = 10, 40, 90, 40, 10 

// second=1,2, 3, 4, 5

std::transform(first, first+5, second, second+5, results, std::divides<int>() ) ;

// results=10, 20, 30, 10, 2


+modulus ; 나눈 나머지. first%second

// numbers= 1,2,3,4,5

std::transform(numbers, numbers+5, remainders, std::bind2nd( std::modulus<int>(), 2) ) ;

// remainders = 1 0 1 0 1


+negate ; nagative   ; -1*x

std::transform(numbers, numbers+5, numbers, std::negate<int>() ) ;

// numbers 값들의 부호를 모두 바꿈.


++비교함수

+equal_to  ; 두 값이 같은 지 확인. 같으면 true, 다르면 false.

// foo = 10,20,30,40,50

// bar = 10,20,40,80,160

// 두 개를 비교하여 처음으로 다른 부분의 값을 각각 구하면

std::pair<int*,int*> ptiter = std::mismatch( foo, foo+5, bar,  std::equal_to<int>() ) ;

// *ptiter.first, *ptiter.second  = 30, 40


+not_equal_to

// 처음으로 나온 다른 값 찾기

// numbers = 10,10,10,20,20

int *pt = std::adjacent_find( numbers, numbers+5, std::not_equal<int>() ) +1 ;

// *pt = 20


+greater

// 내림차순 정렬

std::sort(numbers, numbers+5, std::greater<int>() ) ;    // prev > next


+less

// 오름차순 정렬

std::sort(numbers, numbers+5, std::less<int>() ) ;    // prev < next


+greater_equal

// 0이상인 원소의 개수

cnt = std::count_if( numbers, numbers+5, std::bind2nd(std::greater_equal<int>(), 0) ) ;


+less_equal

// 100이하인 원소의 개수

cnt = std::count_if( numbers, numbers+5, std::bind2nd(std::less_equal<int>(), 100) ) ;


++ 논리 연산

logical_and

logical_or

logical_not

ex) 

bool foo[]={true,false, true,false} ;

bool bar[]={true, true, false, false} ;

bool result[4] ;

std::transform(foo, foo+4, bar, result, std::logical_and<bool>() ) ;

// and 결과 ; true, false, false, false

std::transform(foo, foo+4, bar, result, std::logical_or<bool>() ) ;

// or 결과 ; true, true, true, false

std::transform(foo, foo+4, result, std::logical_not<bool>() ) ;

// not 결과 ; false, true, false, true



+not1  ; 결과를 not한 unary function 객체를 리턴함.

// 홀수가 아닌 원소의 개수.

struct isodd {

bool operator() (const int &x) { return i%2==1 ; }

typedef int argument_type ;

} ;

cx = std::count_if(values, values+5, std::not1(isodd()) ) ;


+not2 ; 결과를 not한 binary function 객체를 리턴함.

firstmismatch = std::mismatch( foo, foo+5, bar, std::not2( std::equal_to<int>() ) ) ;


+ptr_fun ; 함수포인터를 함수객체로 만들어 리턴해 준다.

char* foo[]={"10","20","30"} ;

int bar[3] ;

transform( foo, foo+5, bar, ptr_fun(atoi)) ;


+mem_fun ; 멤버함수를 함수객체로 만들어 리턴해 준다.

vector<string> v ; 

v.push_back( new string("one") ) ;

v.push_back( new string("three") ) ;

vector<int> lens ( v.size() ) ;

transform( v.begin(), v.end(), lens.begin(),  mem_fun( &string::length ) ) ;


+mem_fun_ref ;상동.  레퍼런스 버전.


+ref ; c++11

reference_wrapper 생성자.

객체의 적절한 참조 타입을 생성.

ex)

int foo (10) ;

auto bar = std::ref(foo) ;

bar++ ;

// foo => 11로 업데이트 된다. (참조형인 bar 변경의 영향)















'Develop > C&CPP' 카테고리의 다른 글

utility  (0) 2018.06.08
fstream 1 basic_ifstream  (0) 2018.06.07
algorithm 4 merge, heap, min, max  (0) 2018.06.06
alrorigthm 3 partition, sort, search  (0) 2018.06.05
algorithm 2 copy,swap,transform,replace,fill,generate,remove,unique,..  (0) 2018.06.04

+ Recent posts