c++ 초보입니다. 도와주세요.

trymp의 이미지

c++ 초보입니다.

아래코드를 컴파일 하면 애러가 납니다.

b2.cpp: In function 'int main(int, char**)':
b2.cpp:24: error: assignment of read-only location

왜 이런 애러가 나는지 모르겠습니다

책의 예제에서는 된다고 했는데, 변경 못한다고 애러가 나네요.

도움 부탁드립니다.

#include <iostream>
#include <functional>
#include <set>
 
using namespace std;
 
int main(int ac, char *av[])
{
    set< int > set1;
    set1.insert( 10 );
    set1.insert( 15 );
    set1.insert( 12 );
 
    for( set<int>::iterator IterPos = set1.begin();
            IterPos != set1.end(); ++IterPos )
    {
        cout << *IterPos << endl;
    }
 
    set<int>::iterator FindIter = set1.find( 15 );
    if( FindIter != set1.end() )
    {
        *FindIter = 11;
    }
 
    cout << endl;
    cout << "15를 검색 후11로 변경한 후 set1의 모든 요소 출력" << endl;
    for( set<int>::iterator IterPos = set1.begin();
            IterPos != set1.end(); ++IterPos )
    {
        cout << *IterPos << endl;
    }
 
    return 0;
}
klyx의 이미지

책 예제가 정말 이거 그대로 나와있다면 책이 틀린겁니다. 혹시 set이 아니라 vector나 list인걸 임의로 바꾸신거 아닌가요?
set이나 map의 key는 변경할 수 없습니다. 삽입/삭제만 가능합니다.

hahaite의 이미지

http://www.soen.kr -> 왼쪽강좌의 C/C++ -> 상단의 C/C++ 고급 -> 41장.라. 동등성 조건

여기 예제를 보니 님 책의 예제처럼 키를 변경하는 예제가 있네요.
되는 컴파일러도 있긴 한가봅니다. (구형 컴파일러로 짐작됨)

어쨌든, 마지막 세줄로 요약 가능할 듯 하네요.

만약 키를 꼭 바꾸고 싶다면 어떻게 해야 할까? 이때는 원하는 요소를 일단 삭제하고 키를 변경한 후 다시 삽입하는 방법밖에 없다. 그래야 insert 함수가 변경된 키 위치에 맞게 정렬된 위치를 찾아 제대로 삽입하며 셋은 계속 무결성을 유지할 수 있다. 요점은 셋에 이미 들어가 있는 요소의 키는 절대 바꾸어서는 안된다는 것이다.

^^

trymp의 이미지

..

댓글 달기

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