Thread: std::map Alternatives

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    std::map Alternatives

    Hi,

    In my application I currently use std::map. However, I have found no way to solve the following problems: when an invalid key is passed to operator[] it inserts a new element into the map.

    This is undesirable as my map is rather large to say the least (2-3GiB) in some instances, with 99% of the values being 0 (my map is std::map<std:air<int, int>, int>). Therefore big savings are to be had by assuming a default value of 0.

    But, STL containers do not like being sub-classed and so implementing such functionality is not really possible.

    Therefore, I am interested in alternatives which would support 'default values' for a key. Does anyone have any suggestions? (My application is currently just STL + Boost.)

    Regards, Freddie.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by EvilGuru View Post
    Hi,

    In my application I currently use std::map. However, I have found no way to solve the following problems: when an invalid key is passed to operator[] it inserts a new element into the map.
    Don't use operator[] -- use find(). If the key does not exist, it will return end(). We had another discussion on this topic just this morning.

    Example:

    Code:
    map_type::const_iterator i = my_map.find(some_key);
    if(i == my_map.end())
    {
        // Key not found
    }
    else
    {
        // Key found -- the corresponding item is in:
        i->second;
    }
    You might encapsulate this in a lookup function which returns the default value (0) if the key isn't found.
    Last edited by brewbuck; 04-10-2008 at 11:11 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I actually don't use operator[] for maps very often. Using find works fine.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Here's a generic function that looks up a key in a map and returns the corresponding value, or a default value if the key is not found. You can specify the default to use, otherwise it will use the "default default" which is to default construct the data type. It never inserts anything:

    Code:
    template <typename C>
    typename C::data_type find_or_default(const C &container_p, typename C::key_type key_p,
        typename C::data_type default_p = typename C::data_type ())
    {
        typename C::const_iterator i = container_p.find(key_p);
        if(i == container_p.end()) return default_p;
        return i->second;
    }
    It should work on any mapping type. Have not compiled or tested it. If you data_type does not have a default constructor, you will have to remove the default-default argument (too many occurrences of the word "default!")

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think data_type should be value_type
    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

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    I think data_type should be value_type
    Hmm, I think value_type is actually std:: pair<key_type, data_type>, in other words, the type of object you get when you dereference the iterator..

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm, I think value_type is actually std:: pair<key_type, data_type>, in other words, the type of object you get when you dereference the iterator..
    hmm... that's right. The standard gives mapped_type as the correct typedef.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    hmm... that's right. The standard gives mapped_type as the correct typedef.
    Yeah, I just realized I'm going off the old SGI "standard" instead of the real one. Most STL implementations will provide "data_type" for backward compatibility, but yes -- mapped_type is preferable.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Thanks!

    That did the trick.

    Regards, Freddie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store in std::map all kind of structs
    By umen242 in forum C++ Programming
    Replies: 7
    Last Post: 10-19-2008, 02:24 AM
  2. To tree or not to tree (alternatives to std::map)
    By Sebastiani in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2008, 07:50 AM
  3. Is std::map efficient for this problem?
    By dudeomanodude in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2008, 02:15 PM
  4. std::map question.
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2008, 12:29 PM
  5. passing std::map from one dll to another
    By Carlos in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2003, 07:45 AM