Thread: Editing values of a Map

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Editing values of a Map

    I have inserted some values in a C++ Map, now I want to edit the value associated with a key. I can insert and delete but how to edit?

    Any hints?

    I have tried this:

    Code:
    map <unsigned int, QTcpSocket*> :: iterator objTempIter;
    
    objTempIter = mapName.end ();
    (*objTempIter).first = 22;
    This gives me an error:
    Code:
    error: assignment of read-only data-member ‘std::pair<const unsigned int, QTcpSocket*>::first’
    Last edited by AnishaKaul; 02-03-2011 at 06:06 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    yourMap["keyName"] = newValue;
    Or something like that.
    Devoted my life to programming...

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks for replying,

    Q: Can I edit the key itself?

    Q: The value associated with the key is a socket pointer, so even if I exchange the positions of the current key and its value somehow, then how will I write the keyname according to your example in post 2?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Sorry, i don't have any experience with C++'s map class.

    What i can tell you is that in the code you've tried it says that "first" can't be changed. That's your problem.
    Devoted my life to programming...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AnishaKaul View Post
    Thanks for replying,

    Q: Can I edit the key itself?

    Q: The value associated with the key is a socket pointer, so even if I exchange the positions of the current key and its value somehow, then how will I write the keyname according to your example in post 2?
    Just like every other container, .end() is not an actual element of your map, but rather "one past the last valid item".

    If you want to change the key value, you will have to map the QSocket* to the new int, then remove the previous map element (with something like remove()).

  6. #6
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks TabStop, but I didn't understand the following:
    map the QSocket* to the new int

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, it just means that you add a new element to the map and remove the old one. In this case, adding a new element means that your new integer key will map to the same QTcpSocket pointer as the old integer key.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    I am sorry if I am missing your point again, but did you mean that the previous entry which I want to be edited should be removed and then a fresh entry with the fresh data should be made?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AnishaKaul
    did you mean that the previous entry which I want to be edited should be removed and then a fresh entry with the fresh data should be made?
    Yes, if you want to change the key.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thank you for that, if I have further queries I'll post here again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending Values and Scanning an Array
    By xxxixpats in forum C Programming
    Replies: 7
    Last Post: 11-05-2010, 11:32 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. STL MAP Question
    By deadpoet in forum C++ Programming
    Replies: 11
    Last Post: 02-24-2004, 06:37 AM