도와주세요. multiset

bizzare의 이미지

다음과 같은 코드가 있습니다.
보시면 아시겠지만..
myset 을 만들어서 그 첫번째 항목을 reference로 받고 싶습니다. (물론 예제라서 아직 항목은 넣진 않은 set입니다. )

그런데 윈도우에서는 이러한 코드가 잘 돌아가는데 (VC++ 6.0)
GCC 3.4버전에서는 에러가 나더군요.
그 이유를 살펴보니..
GCC 3.4에서는

iterator와 const_iterator가 둘 다
red black tree의 const_iterator를 typedef 한 녀석이더라고요.

그래서 밑의 코드에서 begin은 iterator를 return하는데
결국은 이 놈은 const reference가 되어
일반 reference에는 대입이 안된다는 에러가 납니다.

하지만 제가 짜고 있는 코드 상에서는 이와 같은 형식의 구문이 꼭 필요한데..
1) iterator도 const_iterator로 되어 있는 것이 올바른 것인지.(즉, GCC 3.4버전의 버그가 아닌지)

2) 만일 올바른 코드라면 어떻게 이러한 문제를 해결할 수 있는지
에 대하여 조언 부탁드립니다.

int main(int argc, char *argv[])
{
  std::multiset<int> myset;
  
  std::multiset<int>::reference ref = *(myset.begin());

  return EXIT_SUCCESS;
}
doldori의 이미지

bizzare wrote:
1) iterator도 const_iterator로 되어 있는 것이 올바른 것인지.(즉, GCC 3.4버전의 버그가 아닌지)

이것 역시 에러 나는 것이 정상입니다. ^^; 그 이유는 당연합니다. set의 value_type은
바로 key_type이기도 하기 때문입니다. 만약 외부에서 마음대로 key를 변경할 수
있다면 항상 정렬된 상태를 유지해야 한다는 요구 조건을 만족할 수 없을 것입니다.

bizzare wrote:
2) 만일 올바른 코드라면 어떻게 이러한 문제를 해결할 수 있는지[/b]
에 대하여 조언 부탁드립니다.

두 가지 상황을 생각할 수 있습니다.
multiset<int> myset;
multiset<int>::iterator i = myset.begin();
int v;
myset.erase(i);
myset.insert(v);

이 때는 기존의 원소를 지우고 새로 삽입하는 수밖에 없습니다.
다른 상황은
struct S
{
    int key_part;
    int value_part;
    bool operator<(const S& rhs) const
    {
        return key_part < rhs.key_part;
    }
};

multiset<S> myset;
multiset<S>::reference ref = const_cast<multiset<S>::reference>(*myset.begin());
// modify ref.value_part only

이 때는 S::key_part에 의해서만 정렬이 되므로 S::value_part만 변경한다는
점만 지키면 문제가 없습니다. 캐스팅이 눈에 거슬리긴 하지만요.

댓글 달기

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