Thread: Change the mapped_value in a std::map

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Change the mapped_value in a std::map

    Hi all,

    My Map container is like this: map<string,int>

    If a certain key value (string) is found,

    I'd like to increment the integer. as in map<string,int>

    How do you go about modifying the mapped value?

    I looked into operator[] but it seems you can only change the key value.

    Any help is greatly appreciated,

    dudeomanodude
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I looked into operator[] but it seems you can only change the key value.
    Actually, you can only change the mapped value, not the key:
    Code:
    ++your_map[your_string];
    Of course, in the above example if your_string is not found in your_map, it would be added with a corresponding mapped value of 0. If this is undesirable, use map's find() member function instead.
    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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by dudeomanodude View Post
    I looked into operator[] but it seems you can only change the key value.
    Look again. Better yet, try it... :-)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    I looked into operator[] but it seems you can only change the key value.
    Changing the key value is impossible. operator[] does exactly what you're looking for.

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Actually, you can only change the mapped value, not the key:
    Sorry, the example on cplusplus.com was a little confusing.

    Yeah it works.

    Now is there any advantage one way or the other between:

    Code:
    ++(*it).second;
    
    // And:
    
    ++map[a_string]
    ???
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    better to use find(key), if you do not want to automatically create new entries for non-existing keys.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now is there any advantage one way or the other between:
    That's not a fair comparison though. It should be between:
    Code:
    std::map<std::string,int>::iterator it = map.find(a_string);
    if (it != map.end())
    {
        ++(*it).second; // or perhaps: ++it->second;
    }
    and:
    Code:
    ++map[a_string];
    In which case the difference is that the former will not insert an element that was not found, whereas the latter will. The advantage depends on the requirements.
    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
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by laserlight View Post
    Actually, you can only change the mapped value, not the key:
    Code:
    ++your_map[your_string];
    Of course, in the above example if your_string is not found in your_map, it would be added with a corresponding mapped value of 1. If this is undesirable, use map's find() member function instead.
    Small edit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Small edit.
    That's incorrect, since the int will be zero initialised when it is added, and then incremented after it is added.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting?
    By greatonesv in forum C Programming
    Replies: 12
    Last Post: 10-22-2008, 08:21 PM
  2. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 07:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM