함수자의 생성자

pok의 이미지

포인터를 원소로 가지는 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());

당연히 컴파일이 안됩니다.

요는, 함수자에 조건을 넘기고 싶은데, 가능한 방법이 무엇일까요?

bugiii의 이미지

erase - remove_if 합성 구문을 이용해야 할 것 같습니다.
그리고, 조건자가 호출될 때 어떤 고정값이 넘어가게 하려면 bind* 류를 이용해서 같이 넘기면 될 것이고요. 지금처럼 생성자에 넣는다는 것은 객체 복사 비용도 그렇고 조금 곤란한 형태가 될 것 같습니다. 요즘 좋은 책이 많이 나오니 한번 보시고, 비슷한 용법을 찾는게 좋겠습니다.

대략 이런식이 되지 않을까 합니다.

dq.erase( remove_if( dq.begin(), dq.end(), bind2nd( BadPred(), badType ) ), dq.end() );

만약, BadPred 의 역할을 멤버 함수로 하고 싶다면 mem_fun 등을 이용해서 지정해주시면 됩니다.

pok의 이미지

감사합니다. 와우. 잘 작동합니다.
BadPred를 static member function으로 했는데 안되길레 ptr_fun을 붙이니 아주 잘되는군요.


poklog at http://poksion.cafe24.com/poklog/

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.