STL - set에서 find_if 쓰려는데.. 힌트 부탁드립니다 ㅎ

lacovnk의 이미지

Class A
{
  public:
   int memberA;
  blabla....
}
set <A*> s;

set s에서 memberA가 특정 값인 원소를 얻으려고 하는데 어렵군요;

생각해본것이..

http://ccl.northwestern.edu/tisue/cs311/fall-00/notes/stl-functions.html
요거에서 보고 function class를 만드려고 했는데 예제에서는 set의 원소를 직접 비교하는 방법이라..

일단 되든 말든 만들었더니 물론 안되더군요 -_-;

class ACompare
{
        private:
                int a;
        public:
                ClientCompare(int newa){this->a = newa;}
                bool operator() (A* a) const {return (a->memberA == this->a}
};

으음. 제가 컨테이너를 잘못 선택한 것일까요?

순서는 상관없고,

1. 컨테이너의 모든 원소에 대해서 일정한 작업을 해야하고
2. 컨테이너의 원소를 검색해야합니다 (위에서 의도하는..)

조언 부탁드립니다 ㅠㅠ

익명 사용자의 이미지

this-> 는 웬만하면 쓰지 마세요 -_-;;

find_if(s.begin(), s.end(), ACompare(특정값))
이걸로 안되나요?

lacovnk의 이미지

혹시 실수가 있을까봐 코드를 적습니다. ㅎㅎ

class ClientCompare
{
        private:
                int socket;
        public:
                ClientCompare(int socket){this->socket = socket;}
                bool operator() (Client* client) const {return (client->tcpSocket->isSocket(this->socket));}
};
Client*
ClientSET::getClientBySocket(int socket)
{
        Client* result;
        ClientCompare* clientCompare = new ClientCompare(socket);
        result = find_if(this->clientSet.begin(),this->clientSet.end(),ClientCompare(socket));
        delete clientCompare;
        return result;
}

Quote:
chatd.cc: In member function `Client* ClientSET::getClientBySocket(int)':
chatd.cc:63: error: cannot convert `std::_Rb_tree_iterator<Client*, Client*
const&, Client* const*>' to `Client*' in assignment

덧. 전 명시적으로 this를 일부러 쓰고 있었는데.. 어떤 이유가 있는건가요? 궁금해요~ ㅎ (구글에서 this가 일반적인 단어라 검색이 안되는군요 ㅎ)

lnsium의 이미지

일단 find_if는 시퀀스 알고리즘이라 set에 적용하는 것은 비추입니다. set은 자신만의 find멤버 함수가 있지요.
굴구 set<A*>이렇게 하는 것은 set을 쓸 이유를 날려버립니다.
트리를 구성할 때 주소값의 대소를 가지고 하는데 이건 set을 쓰는 이유가 될 수 없습니다. 구지 한다면
set<A*, (여기에 알맞는 비교 함수 객체)>을 지정해 줘야합니다.

글구..함수 객체의 사용은 이상이 없어 보입니다..-.-;;

lnsium의 이미지

ClientSET::getClientBySocket(int socket) 
{ 
        Client* result; 
        ClientCompare* clientCompare = new ClientCompare(socket); 
        result = find_if(this->clientSet.begin(),this->clientSet.end(),ClientCompare(socket)); 
        delete clientCompare; 
        return result; 
} 

find_if는 반복자를 리턴합니다.

익명 사용자의 이미지

clientCompare 를 동적으로 할당해놓고 사용을 안하시는군요 -_-;
그리고 find_if의 리턴타입은 set::iterator 이지 Client* 가 아닙니다.
iterator 를 Client* 타입에 대입하려고 해서 에러가 나는군요.

lacovnk의 이미지

감사합니다 ㅠㅠ

지적을 듣고나서야 뭔가 이상하다는게 스르륵.. 이 아니라 파박 느껴지는군요

stl쪽 더 살펴봐야겠습니다; man이 없으니 번거롭군요;

http://oopsla.snu.ac.kr/~sjjung/stl/sel_2116.htm

요거 보고 다시 골라봐야겠습니다 으흐흐 :oops:

댓글 달기

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