[질문] < 연산자 정의 질문입니다.

chadr의 이미지

stl의 map의 키로 몇개의 멤버를 가진 구조체를 사용할려고 합니다.

그런데 map에서 키값의 비교에 사용할 < 연산자를 정의해야하는데 간단한 방법이 떠오르지 않아 질문드립니다.

우선 키로 사용될 구조체는 아래와 같습니다.

typedef struct tagFONT_RENDER_OPTION
    {
      tagFONT_RENDER_OPTION()
      {
        bBold = false;
        bMiddleLine = false;
        bUnderLine = false;
      }
 
      bool bBold;
      bool bUnderLine;
      bool bMiddleLine;
 
    }FONT_RENDER_OPTION;

std:pair에 보면 구조체가 멤버를 두개 가지고 있을때는 아래와 같이 정의하더군요.

bool operator<(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left < _Right for pairs
	return (_Left.first < _Right.first ||
		!(_Right.first < _Left.first) && _Left.second < _Right.second);
	}

위와 같은 정의라면 멤버가 하나 늘어날때마다 코드가 정말 안드로메다로 갈게 분명합니다..

이런식으로 구조체의 멤버가 여러개일때 < 연산자를 정의 하는 방법중 간단한 방법은 없을까요?

klara의 이미지

std::pair의 정의역시 그냥 pair의 요소의 비교 연산자를 이용하는 것뿐이고, 템플릿이라 복잡해보이는 것같은데요...

<를 정의한다는건 그냥 연산자 오버로드를 이용하면 될뿐인것이고, 어떻게 정의하느냐는 구조체1 < 구조체2 가 무엇을 의미하는지가 중요한것 같습니다.

예를 들어 학생을 나타내기 위해 Student 라는 클래스를 만들고, 여기에 학생 번호를 나타내는 int number라는 멤버변수와 이름을 나타내는 std::string name이라는 멤버변수, 그리고 성적을 나타내는 int grade 라는 멤버변수가 있을때, 이 클래스의 <를 오버로드한다고 하면, 먼저 생각할께 학생과 학생을 <로 비교한다는게 무엇을 의미하는지를 결정해야 합니다.

student1 < student2 의 의미가 student2의 성적이 더 좋다는 걸 나타낸다면, < 오버로드의 내용은 return student1.grade < student2.grade; 가 될테고, 만약 student1이 앞번호라는 의미라면 return student1.number < student2.number가 될것입니다.

chadr의 이미지

유일한 키를 만드는게 목적입니다.
멤버들의 타입은 클래스가 아닌 내장타입들입니다.

작다는 것의 의미는 같은 키와 비교를 했을 때 멤버들의 실제 메모리의 데이터구조상
"작다"라는 의미가 되면 됩니다.

그렇기 때문에 모든 멤버들이 전부 비교가 되어야합니다..

이런 의미에서 std:pair의 < 연산자의 구현이 제가 원하는 목적에 부합되긴합니다만..(모든 멤버가 작다라는 의미를 표현하기 위해 포함이 되었기 때문에)

멤버가 두개일 때

(_Left.first < _Right.first ||
!(_Right.first < _Left.first) && _Left.second < _Right.second)

이런코드가 만들어지는데 처음 조건인

(_Left.first < _Right.first ||
!(_Right.first < _Left.first)

가 멤버가 1개 늘어날때마다 이런 형식이 적용되어 안습코드가 만들어지게 되므로
좀더 간단한 방법이 없을까라는 생각이 들었습니다.

이럴경우 좀더 간단하게, 모든멤버들이 전부 비교가 되면서 작다 라는 의미를 가지게 할려면
어떻게 하는지 궁금합니다. :)
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

익명 사용자의 이미지

반드시 필요한 연산이기 때문에 더 간단하게 할 수 있는 방법은 없습니다.
어차피 같은 패턴으로 반복되는 것이니 어렵다기보다는 귀찮아서 그러시는 듯하군요.

struct Temporary
{
      bool bBold;
      bool bUnderLine;
 
      bool operator<(const Temporary&) const;
};
 
struct FONT_RENDER_OPTION
{
      Temporary temp;
      bool bool bMiddleLine;
 
      bool operator<(const FONT_RENDER_OPTION&) const;
};

이런 식으로 하면 std::pair와 똑같은 방식으로 < 연산을 구현할 수는 있겠습니다만
글쎄요... 별로 아름답지는 않군요. -_-;

댓글 달기

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