함수자의 생성자
글쓴이: pok / 작성시간: 수, 2006/11/01 - 11:15오전
포인터를 원소로 가지는 deque에서 일치하는 값을 지우려고 합니다.
술어역할을 하는 함수자에 내용물 이외의 값을 넣으려는게 목적입니다.
일단은 함수자에 대해 잘 몰라서 생성자를 이용하면 되지 않을까 해서 이런코드를 작성했습니다.
//deleting functor with partition class FDeletePartion : public unary_function<CCommandBuffer*, bool> { public: FDeletePartion(CCommandBuffer::COMMAND_TYPE _type){ m_type_ = _type; } //FDeletePartion(){} ~FDeletePartion(){} bool operator()(CCommandBuffer* _pBuffer) { if(_pBuffer->m_eCmdType == m_type_) { SAFE_DELETE(_pBuffer); return true; } else return false; } private: CCommandBuffer::COMMAND_TYPE m_type_; }; ... std::deque<CCommandBuffer*> m_dqCommandBuffer_; ... //remove command queue m_dqCommandBuffer_.erase(partition(m_dqCommandBuffer_.begin(), m_dqCommandBuffer_.end(), FDeletePartion(CCommandBuffer::TYPE_A)()) , m_dqCommandBuffer_.end());
당연히 컴파일이 안됩니다.
요는, 함수자에 조건을 넘기고 싶은데, 가능한 방법이 무엇일까요?
Forums:
erase - remove_if 합성
erase - remove_if 합성 구문을 이용해야 할 것 같습니다.
그리고, 조건자가 호출될 때 어떤 고정값이 넘어가게 하려면 bind* 류를 이용해서 같이 넘기면 될 것이고요. 지금처럼 생성자에 넣는다는 것은 객체 복사 비용도 그렇고 조금 곤란한 형태가 될 것 같습니다. 요즘 좋은 책이 많이 나오니 한번 보시고, 비슷한 용법을 찾는게 좋겠습니다.
대략 이런식이 되지 않을까 합니다.
dq.erase( remove_if( dq.begin(), dq.end(), bind2nd( BadPred(), badType ) ), dq.end() );
만약, BadPred 의 역할을 멤버 함수로 하고 싶다면 mem_fun 등을 이용해서 지정해주시면 됩니다.
오호..
감사합니다. 와우. 잘 작동합니다.
![](http://kldp.net/themes/kldp/images/tabs/topright-dark.png)
BadPred를 static member function으로 했는데 안되길레 ptr_fun을 붙이니 아주 잘되는군요.
poklog at http://poksion.cafe24.com/poklog/
poklog at http://poksion.cafe24.com/poklog/
댓글 달기