LIST 자료 구조에서, 사용자 정의 구조체의 원소 찾기.

woox의 이미지

안녕하세요?

STL 첨이라 보니, 좀 힘드네요.

삼차원 데이터 정보. 즉
(x,y,z,v) 즉 x,y,z 위치의 값 v 를 표현하기 위해서,

typedef struct {
	float x ;
	float y ;
	float z ;
	float value ;
} __attribute__ ((packed)) _PointValue ; 

이렇게 정의하였습니다.

포인트 갯수가 약 15000 ~ 20000 개 정도되는데,
프로그램의 문맥상 각 데이터들 사이에 5개나 혹은 몇개씩
더 들어 갈수가 있습니다.
그래서, STL 책을 읽다가, list 가 맞지 않나 생각해서
list 로 잡았습니다.


 list<_PointValue> PointValue ;
 ...
 //PointValue 에 값을 넣습니다.
 //데이터 파일을 읽어서 아래와 같이 데이터를 
 //list 에 하나하나씩 넣습니다.
 _PointValue tmp_val ;
 tmp_val.x = 0 ;
 tmp_val.y = 0 ;
 tmp_val.z = 0 ;
 tmp_val.value = 8 ;  

 PointValue.push_back(tmp_val);

 PointValue.find(?,?,?) ;

제가 여기서 궁금한 점은 만약에 x=5, y=3, z=3 인 점의
value를 찾고 싶다고 할때는 어떻게 해야 하는게 좋을까요?
ㅤㅇㅣㅌ레이터 돌면 되겠지만, 제공해주는 방법은 없을까요?

읽어주셔서 감사합니다.

ixevexi의 이미지

1. 우선 list 템플릿은 find를 제공해주지 않습니다.
STL의 제네릭한 특성상 find라는 함수는 알고리즘에 속하기 때문에
find( begIter, endIter, Element)
이런식으로 쓰셔야 합니다.

물론 find를 제공하는 컨테이너도 있습니다.( map/multimap, set/multiset)

2. 원하시는 걸 구현하려면 _PointValue에 ==오퍼레이터를
재정의 하시면 됩니다.
간단히

bool _PointValue::operator ==( const _PointValue& rhs) const
{
    if(  (x ==rhs.x) && (y== rhs.y)&& (z==rhs.z) )
        return true;
    return false;
}

이 메쏘드를 추가하시면 됩니다.

3. 그래서 예를 들어 3,5,4 를 찾고 싶다.

list<_PointValue>::iterator iter =
find( Pointvalue.begin(), PointValue.end(), _PointValue(3,5,4) )

이런식으로 쓰시면 됩니다.

생각해보니 _PointValue에 3개의 인자를 가지는 생성자를
하나 더 추가하셔야 겠네요

결과적으로


class _PointValue
{
 private:
    float x,y,z, value;
 public:
   _PointValue(float _x=0, float _y=0, float _z=0)
     :x(_x),y(_y),z(_z)
     {}
   bool operator == ( const _PointValue& rhs);
}


이렇게 되겠네요

C++, 그리고 C++....
죽어도 C++

익명 사용자의 이미지

방법이 여러가지 있겠지만, 단순히 찾는 거라면 find_if 를 쓰시면 됩니다.

간단하게 테스트 용으로...

#include <iostream>
#include <list>
#include <algorithm>

struct PointValue {
    explicit PointValue(float x = 0.0f, float y = 0.0f, float z = 0.0f, float value = 0.0f)
        : x_(x), y_(y), z_(z), value_(value)
    {}

    float x_;
    float y_;
    float z_;
    float value_;
};

struct FindPoint : public std::binary_function<PointValue, PointValue, bool> {
    bool operator()(const PointValue& lhs, const PointValue& rhs) const
    {
        if(lhs.x_ == rhs.x_ &&
           lhs.y_ == rhs.y_ &&
           lhs.z_ == rhs.z_)
            return true;
        else
            return false;
    }
};

void InputForTest(std::list<PointValue>& dest)
{
    dest.push_back(PointValue(1.0f, 1.0f, 1.0f, 1.0f));
    dest.push_back(PointValue(2.0f, 1.0f, 3.0f, 1.1f));
    dest.push_back(PointValue(1.0f, 1.0f, 1.0f, 1.2f));
    dest.push_back(PointValue(2.0f, 1.0f, 3.0f, 1.3f));
    dest.push_back(PointValue(5.0f, 3.0f, 3.0f, 1.4f));
    dest.push_back(PointValue(1.0f, 1.0f, 1.0f, 1.5f));
    dest.push_back(PointValue(2.0f, 1.0f, 3.0f, 1.6f));
    dest.push_back(PointValue(1.0f, 1.0f, 1.0f, 1.7f));
    dest.push_back(PointValue(2.0f, 1.0f, 3.0f, 1.8f));
    dest.push_back(PointValue(5.0f, 3.0f, 3.0f, 1.9f));
    dest.push_back(PointValue(1.0f, 1.0f, 1.0f, 2.0f));
}

void PrintValue(const std::list<PointValue>& dest, float x, float y, float z)
{
    PointValue pointToFind(x, y, z);

    std::list<PointValue>::const_iterator find(dest.begin());
    const std::list<PointValue>::const_iterator end(dest.end());

    for(;;) {

        find = std::find_if(find, end, std::bind2nd(FindPoint(), pointToFind));

        if(find == end)
            break;

        std::cout << find->value_ << std::endl;

        ++find;
    }
}

int main(void)
{
    std::list<PointValue> pointValueList;

    InputForTest(pointValueList);

    PrintValue(pointValueList, 5.0f, 3.0f, 3.0f);

    return 0;
}
woox의 이미지

아~ 그렇군요.
정말 감사합니다. 이제 어떻게 하면 될지 알겠네요.
저는 한참 고민했는데.

좋은 하루 되세요.

girneter의 이미지

typedef struct {
	float x ;
	float y ;
	float z ;
	float value ;
} __attribute__ ((packed)) _PointValue ; 

원질문에서
__attribute__ 은 무엇이고
(packed) 는 무엇인가요?

C++ 의 예약어인가요? 첨 보는데

개념없는 초딩들은 좋은 말로 할때 DC나 웃대가서 놀아라. 응?

익명 사용자의 이미지

Quote:
__attribute__ 은 무엇이고
(packed) 는 무엇인가요?

구조체를 패킹할때, 위와 같은 옵션이 있을때랑 없을때랑 차이가
있습니다. (C++의 예약어가 아니라 C 입니다.)

만약에
typedef struct {

float x ;
float y ;
float z ;
double value;

} points ;

이렇게 되었을때와

typedef struct {

float x ;
float y ;
float z ;
double value;

} __attribute__ ((packed)) points ;

를 sizeof (points) 해보시면, 그 결과를 아실수 있으리라 생각됩니다.
points 수가 만개에서 이만개로 예상되기 때문에 위와 같은 옵션을
주었습니다.

ixevexi의 이미지

Anonymous wrote:
Quote:
__attribute__ 은 무엇이고
(packed) 는 무엇인가요?

(C++의 예약어가 아니라 C 입니다.)

오해의 여지가 있을 듯 해서요.
gcc extension 아닌가요? C99에는 없지 않나요?

C++, 그리고 C++....
죽어도 C++

댓글 달기

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