Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:01:43+00:00 2026-05-17T01:01:43+00:00

The minimal example of the problem I’m having is reproduced below: #include <set> using

  • 0

The minimal example of the problem I’m having is reproduced below:

#include <set>
using namespace std;

class foo {
public:
  int value, x;
  foo(const int & in_v) {
   value = in_v;
   x = 0;
  }
  bool operator<(const foo & rhs) const {
   return value < rhs.value; 
 }
};

int main() {
  foo y(3);
  set<foo> F;
  F.insert(y);

  // Now try to modify a member of the set
  F.begin()->x=1;
  return 0;
}

With the error error: assignment of data-member ‘foo::value’ in read-only structure. I feel like I’m missing something simple here, but why am I unable to modify the member x in my class?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:01:44+00:00Added an answer on May 17, 2026 at 1:01 am

    Objects in a set are immutable; if you want to modify an object, you need to:

    1. make a copy of the object from the set,
    2. modify the copy,
    3. remove the original object from the set, and
    4. insert the copy into the set

    It will look something like this:

    std::set<int> s;
    s.insert(1);
    
    int x = *s.begin(); // (1)
    x+= 1;              // (2)
    s.erase(s.begin()); // (3)
    s.insert(x);        // (4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a minimal example which illustrates my problem test.c: #include <stdio.h> #include <pthread.h>
A minimal code that reproduces the problem is as follows: <div class=cell> <input type=text
Anyone have an minimal example of ASP.NET MVC and dotnetcharting.com control lying around? Thanks,
EDIT I isolated a real minimal example which does not work (it is a
I'm trying to customise the sc-player-minimal.html example included with the JS custom player package.
Minimal code: // --------inline.h-------- struct X { static inline void foo (); }; #ifdef
I've got a minimal app I just created, using VS 2008 SP1 on Vista
I'm writing a minimal web browser using Cocoa. I've been struggling in my attempt
Minimal example dialog: <p:dialog header=Test Dialog widgetVar=testDialog> <h:form> <p:inputText value=#{mbean.someValue}/> <p:commandButton value=Save onsuccess=testDialog.hide() actionListener=#{mbean.saveMethod}/>
I have a problem with giving a template class a template friend in Sun

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.