함수객체를 이용하여 구조체에서 특별한 값 검색하기

purewell의 이미지

함수객체를 이용하여 구조체에 특별한 값을 검색하는데 std::find를 이용하고 싶습니다.

예)

typedef struct _ITEM
{
  int id;
  string name;
} ITEM;
 
typedef vector<ITEM> ITEM_VEC;
ITEM_VEC v;
 
class _finder
{
public:
  _finder(int _id):id(_id) {}
  bool operator () (const ITEM& itm) const
  {
    return (itm.id == id);
  }
private:
  const int id;
};
 
// 중략...
 
find_if(v.begin(), v.end(), _finder(10)); // <- 이런 식으로 찾기

이런 식으로 찾으려고 했는데 컴파일 에러를 퉤! 벹어내는군요.

error: no matching function for call to `find_if(std::_List_iterator<_ITEM>, std::_List_iterator<_ITEM>, _finder)'

어떻게 하면 되죠? 예전에 뭐시기 STL 책에서 본 것 같은데 까먹어서리...

thyoo의 이미지

별 이상 없어 보입니다.

에러 메세지가 좀 이상하군요.
find_if가 아니라 find를 못 찾겠다고 하는데요.

ITEM structure를 맘대로 할 수 있는 상황이라면

struct ITEM {
	int id;
	// ...
	operator int() const { return id; }
};
 
vector<ITEM>::iterator it2 = find(v.begin(), v.end(), 2);

___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

purewell의 이미지

처음에 find로 했다가 find_if로 다시 했습니다. 수정하였습니다.
그러나, 아직 해결은 못했다는거~
_____________________________
언제나 맑고픈 샘이가...
http://yubink.com - 강아지 필요하세요?
http://purewell.biz - 헙!!

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

purewell의 이미지

저기에 명시되지 않은 사항입니다.
_finder 클래스는 함수 안에서 정의한 클래스입니다.

bool finder(......)
{
  class _finder {...};
  find_if(...., _finder(...));
  ...
};

컴파일이 안 됩니다.

_finder를 함수 밖으로 빼내면 컴파일 잘~ 됩니다.
_____________________________
언제나 맑고픈 샘이가...
http://yubink.com - 강아지 필요하세요?
http://purewell.biz - 헙!!

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

thyoo의 이미지

Local Type을 template argument로 사용하는 것을
표준이 금지하고 있읍니다.

14.3.1 Template type arguments
2 A local type, a type with no linkage, an unnamed type or
a type compounded from any of these types shall not be used
as a template-argument for a template type-parameter.

Compiler가 template을 전개해서 함수를 만들어야 하는데
함수 안에서 함수를 정의할 수 있으면 몰라도
호출하는 함수 밖에 전개 정의할 수 밖에 없읍니다.
한데, 보이지 않는 Local Type으로 함수를 정의하면
안되지 않습니까.
___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

purewell의 이미지

* o*) 아항! 생각해보니 그렇군요.
답변 아주매우많이 감사드립니다.
_____________________________
언제나 맑고픈 샘이가...
http://yubink.com - 강아지 필요하세요?
http://purewell.biz - 헙!!

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.