stl의 binary_search함수를 한번 써보고 싶습니다.

gugudan의 이미지

#include <list>
#include <algorithm>
#include <iterator>
struct _DATA
{
public:
    int x;
    int y;
    int operator < (_DATA & data)
    {
           return (x < data->x);
    }

//bool operator<(const _DATA& r) const { return x < r.x ? true : false;} 
};



int main()
{
    list<_DATA> m_data;
    _DATA data;

    data.x = 1;
    data.y = 2;

    m_data.push_back(data);


    _DATA data2;
    data2.x=3;
    data2.y=4;

    bool finder = std::binary_search(m_data.begin(),m_data.end(),data2);


    return 1;
}

이런식으로 하는게 아닌가요?
예제들을 보면..
전부 그냥 int형 변수를 search 하는거 밖에 보이질 않네요..

그럼 수고하십시요.

yielding의 이미지

말씀하신데로 사용하시면 임의의 자료형에 대해서 사용가능하고요.

binary_search는 컨테이너가 sort되어 있다는 가정에서 사용하는 것도 기억하셔야합니다.

Life rushes on, we are distracted

doldori의 이미지

bool operator < (const _DATA & data)

로 바꾸세요.

익명 사용자의 이미지

find_list.cpp: In method `int _DATA::operator< (const _DATA &)':
find_list.cpp:11: base operand of `->' has non-pointer type `const
_DATA'
/usr/include/g++-3/stl_algo.h: In function `bool binary_search
(_ForwardIter, _ForwardIter, const _Tp &) [with _ForwardIter =
_List_iterator<_DATA, _DATA &, _DATA *>, _Tp = int]':
find_list.cpp:32: instantiated from here
/usr/include/g++-3/stl_algo.h:1927: no match for `const int & < _DATA
&'
/usr/include/g++-3/stl_algo.h: In function `_ForwardIter __lower_bound
(_ForwardIter, _ForwardIter, const _Tp &, _Distance *) [with
_ForwardIter = _List_iterator<_DATA, _DATA &, _DATA *>, _Tp = int,
_Distance = ptrdiff_t]':
/usr/include/g++-3/stl_algo.h:1750: instantiated from `lower_bound (_ForwardIter, _ForwardIter, const _Tp &) [with _ForwardIter = _List_iterator<_DATA, _DATA &, _DATA *>, _Tp = int]'
/usr/include/g++-3/stl_algo.h:1926: instantiated from `binary_search (_ForwardIter, _ForwardIter, const _Tp &) [with _ForwardIter = _List_iterator<_DATA, _DATA &, _DATA *>, _Tp = int]'
find_list.cpp:32: instantiated from here
/usr/include/g++-3/stl_algo.h:1735: no match for `_DATA & < const int
&'
find_list.cpp:10: candidates are: int _DATA::operator< (const _DATA &)

doldori의 이미지

윽... 답변에서 함수 프로토타입이 잘못 됐군요. 그리고 함수 인자는 참조형이므로
->가 아니라 . 으로 해야 합니다.

struct _DATA
{
    bool operator < (const _DATA & data) const
    { 
           return (x < data.x); 
    } 
};
cdpark의 이미지

operator는 class의 memeber 함수로 만드는 것보다는 friend 함수로 만드는 습관을 들이는게 어떨까요?

익명 사용자의 이미지

list<_DATA>::iterator pos = find(m_data.begin(),m_data.end(),1);

왜 이런식으로 했는데 에러가 발생하는지 모르겠네요..

값을 어떻게 줘야 하는지요?
위와 같이 해도 에러가 나오고
_DATA data2;
data2.x=3;
data2.y=4;

list<_DATA>::iterator pos = find(m_data.begin(),m_data.end(),data2)
이렇게 해도 에러가 나오네요.

doldori의 이미지

어떤 값을 찾기 위해서는 두 값을 비교하는 연산이 필요한데, find는 동일함(equality),
binary_search 같이 정렬과 관련이 있는 알고리듬이나 컨테이너는 strict weak
ordering에 의한 동등함(equivalence)이 기준입니다. 따라서 binary_search를
사용하려면 < 연산이, find를 쓰려면 == 연산이 정의되어야 합니다.

struct _DATA
{
    bool operator==(const _DATA& r) const
    {
        return x == r.x && y == r.y;
    }
};
gugudan의 이미지

많은 도움 되었습니다.

댓글 달기

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