Contents
1. <algorithm> 멤버 [Bottom] [Top]
#include <algorithm>
1.1. for_each() 함수 [Bottom] [Top]
- 사용법
for_each( 시작, 끝, 함수 또는 함수객체 )
template< class InputIterator, class Function > Function for_each( InputIterator first, InputIterator last, Function f ) { while( first != last ) f( *first++ ); return f; }
1.1.1. 사용 예 [Bottom] [Top]
- 클래스 및 데이터형 선언
//------------------------------------------------------------------------------ // 클래스 선언 class CData { ... }; //------------------------------------------------------------------------------ // 데이터형 선언 typedef std::vector< CData * > VEC_DATA; typedef stdext::hash_map< int, CData * > MAP_DATA; typedef std::pair< int, CData * > MAP_VALUE_DATA;
- vector, list, set, ... 사용
VEC_DATA vecData; // for_each 사용 for_each( vecData.begin(), vecData.end(), _ViewData ); // _ViewData() 함수 구현 void _ViewData( CData * pData ) { ... }
- map, hash_map, ... 사용
MAP_DATA mapData; // for_each 사용 for_each( mapData.begin(), mapData.end(), _ViewData ); // _ViewData() 함수 구현 (주의!: 함수의 인수형) void _ViewData( MAP_VALUE_DATA data ) { ... }
1.1.2. for_each() 함수 응용 [Bottom] [Top]
- 예제 클래스 선언 및 초기화
//------------------------------------------------------------------------------ // 클래스 선언 (데이터) class CData { // 생성자 public: CData( int nData ) : m_nData( nData ) {} // 멤버 함수 public: ... void funA() { ... } ... int GetData() { return m_nData; } // 멤버 변수 protected: int m_nData; }; //------------------------------------------------------------------------------ // 클래스 선언 (데이터 처리) class CTransactor { // 생성자 public: ... // 멤버 함수 public: ... void funB( CData * pData ) { ... } // 멤버 변수 protected: ... }; //------------------------------------------------------------------------------ // 클래스 선언 (함수 객체) class CFunc { // 생성자 public: CFunc() : m_nTotal( 0 ), m_nCount( 0 ) {} // 일반 생성자 CFunc( const CFunc & obj ) // 복사 생성자 { m_nTotal = obj.m_nTotal; m_nCount = obj.m_nCount; } // 범버 함수 (연산자 재정의) public: void operator() ( CData * pData ) { m_nTotal += pData->GetData(); ++m_nCount; } public: int GetTotal() { return m_nTotal; } int GetAverage() { return m_nTotal / m_nCount; } // 멤버 변수 protected: int m_nTotal; int m_nCount; }; //////////////////////////////////////////////////////////////////////////////// // 데이터 초기화 vector< CData * > c; for( int i=0; 10>i; ++i ) { c.push_back( new CData( i ) ); }
1.1.2.1. 응용 1: 컨테이너에 들어 있는 객체의 멤버 함수 호출 [Bottom] [Top]
// CASE 1: 컨테이너에 들어 있는 객체의 funA() 함수(멤버 함수)를 호출한다. for_each( c.begin(), c.end(), mem_fun( &CData::funA ) ); for_each( c.begin(), c.end(), mem_fun_ref( &CData::funA ) );
- 코드 설명
mem_fun( &CData::funA ) - pData->funA() 형태로 실행.
mem_fun_ref( &CData::funA ) - pData.funA() 형태로 실행.
- 반환값이나 인수가 있는 경우
mem_fun_t< Result, Type > - Result Type::func() 형태로 실행.
mem_fun1_t< Result, Type, Arg > - Result Type::func( Arg ) 형태로 실행.
const_mem_fun_t< Result, Type > - Result Type::func() const 형태로 실행.
const_mem_fun1_t< Result, Type, Arg > - Result Type::func( Arg ) const 형태로 실행.
1.1.2.2. 응용 2: 다른 객체의 멤버 함수로 컨테이너에 들어 있는 객체를 인수로 전달하며 호출 [Bottom] [Top]
CTransactor a; // CASE 2: 객체 a의 funB() 함수(멤버 함수)로 컨테이너에 들어 있는 객체를 인수로 // 전달하며 호출한다. for_each( c.begin(), c.end(), bind1st( mem_fun( &CTransactor::funB ), &a ) );
- 코드 설명
bind1st( mem_fun( &CTransactor::funB ), &a ) - a->funB( pData ) 형태로 실행.
1.1.2.3. 응용 3: for_each() 함수의 처리 결과를 객체로 반환 [Bottom] [Top]
// CASE 3: for_each() 함수의 처리 결과를 CFunc 객체로 반환한다. CFunc b = for_each( c.begin(), c.end(), CFunc() ); printf( "Total\t: %d\n", b.GetTotal() ); printf( "Average\t: %d\n", b.GetAverage() );
- 코드 설명
for_each() 함수의 처리 결과를 CFunc 객체로 반환. (주의! 인수로 전달되는 객체와 반환되는 객체는 동일형이어야 함.)
for_each() 함수의 인수로 전달되는 함수 객체는 operator() 연산자를 재정의해야 됨(필수 사항).
참고> 함수 객체를 인수 전달 시 생성자 호출.
for_each() 함수로 진입 시 - 일반 생성자 또는 복사 생성자가 실행됨(객체 복사가 일어남).
for_each() 함수에서 반환 시 - 복사 생성자가 실행됨(객체 복사가 일어남).
1.2. transform() 함수 [Bottom] [Top]
- 사용법
transform( 시작A, 끝A, 시작B, 함수 또는 함수객체 ) // 단항연산 transform( 시작A, 끝A, 시작B, 끝B, 함수 또는 함수객체 ) // 이항연산
2. <functional> 멤버 [Bottom] [Top]
#include <functional>
2.1. bind1st() 함수 [Bottom] [Top]
매개변수가 2개인 함수 (이항함수) 에서 첫번째 매개변수를 맵핑한다.
2.2. bind2nd() 함수 [Bottom] [Top]
매개변수가 2개인 함수 (이항함수) 에서 두번째 매개변수를 맵핑한다.
2.3. ptr_fun() 함수 [Bottom] [Top]
일반함수를 함수객체로 변환한다.
2.4. mem_fun() 함수 [Bottom] [Top]
객체의 맴버함수 포인터를 함수객체로 변환한다.
template< class Result, class Type > mem_fun_t< Result, Type > mem_fun( Result ( Type::*_Pm )() ); template< class Result, class Type, class Arg > mem_fun1_t< Result, Type, Arg > mem_fun( Result ( Type::*_Pm )( Arg ) ); template< class Result, class Type > const_mem_fun_t< Result, Type > mem_fun( Result ( Type::*_Pm )() const ); template< class Result, class Type, class Arg > const_mem_fun1_t< Result, Type, Arg > mem_fun( Result ( Type::*_Pm )( Arg ) const );
- 어댑터 클래스 (Adapter Class)
template< class Result, class Type > struct mem_fun_t : public unary_function< Type *, Result > { explicit mem_fun_t( Result ( Type::*_Pm )() ); Result operator()( Type* _Pleft ) const; }; template< class Result, class Type, class Arg > struct mem_fun1_t : public binary_function< Type *, Arg, Result > { explicit mem_fun1_t( Result ( Type::*_Pm )( Arg ) ); Result operator()( Type* _Pleft, Arg _Right ) const; }; template< class Result, class Type > struct const_mem_fun_t : public unary_function< Type *, Result > { explicit const_mem_fun_t( Result ( Type::*_Pm )() const ); Result operator()( const Type* _Pleft ) const; }; template< class Result, class Type, class Arg > struct const_mem_fun1_t : public binary_function< const Type *, Arg, Result > { explicit const_mem_fun1_t( Result ( Type::*_Pm )( Arg ) const ); Result operator()( const Type* _Pleft, Arg _Right ) const; };
2.5. mem_fun_ref() 함수 [Bottom] [Top]
객체의 멤버함수 참조를 함수객체로 변환한다.
template< class Result, class Type > mem_fun_ref_t< Result, Type > mem_fun_ref( Result ( Type::*_Pm )() ); template< class Result, class Type, class Arg > mem_fun1_ref_t< Result, Type, Arg > mem_fun_ref( Result ( Type::*_Pm )( Arg ) ); template< class Result, class Type > const_mem_fun_ref_t< Result, Type > mem_fun_ref( Result ( Type::*_Pm )() const ); template< class Result, class Type, class Arg > const_mem_fun1_ref_t< Result, Type, Arg> mem_fun_ref( Result ( _Type::*_Pm )( Arg ) const );
- 어댑터 클래스 (Adapter Class)
template< class Result, class Type > struct mem_fun_ref_t : public unary_function< Type, Result > { explicit mem_fun_t( Result ( Type::*_Pm )() ); Result operator()( Type& _Left ) const; }; template< class Result, class Type, class Arg > struct mem_fun1_ref_t : public binary_function< Type, Arg, Result > { explicit mem_fun1_ref_t( Result ( Type::*_Pm )( Arg ) ); Result operator()( Type& _Left, Arg _Right ) const; }; template< class Result, class Type > struct const_mem_fun_ref_t : public unary_function< Type, Result > { explicit const_mem_fun_t( Result ( Type::*_Pm)() const ); Result operator()( const Type& _Left ) const; }; template< class Result, class Type, class Arg > struct const_mem_fun1_ref_t : public binary_function< Type, Arg, Result > { explicit const_mem_fun1_ref_t( Result (Type::*_Pm )( Arg ) const ); Result operator()( const Type& _Left, Arg _Right ) const; };
