Change the mapped_value in a std::map

This is a discussion on Change the mapped_value in a std::map within the C++ Programming forums, part of the General Programming Boards category; Hi all, My Map container is like this: map<string,int> If a certain key value (string) is found, I'd like to ...

  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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    839
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,799
    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
    21,636
    Small edit.
    That's incorrect, since the int will be zero initialised when it is added, and then incremented after it is added.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 02:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21