std::map Alternatives

This is a discussion on std::map Alternatives within the C++ Programming forums, part of the General Programming Boards category; Hi, In my application I currently use std::map. However, I have found no way to solve the following problems: when ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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,317
    I actually don't use operator[] for maps very often. Using find works fine.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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
    20,954
    I think data_type should be value_type
    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

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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
    20,954
    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.
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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

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