Thread: Searching STL Map Inside STL Map Object :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Searching STL Map Inside STL Map Object :: C++

    Hi.

    How do you know if find() returns a valid element in a map that is inside a map? For example:

    Code:
    typedef std::map<int, int> mapIntInt;
    typedef std::map<double, mapIntInt> mapDM;
    
    mapIntInt mapOne;
    mapOne.insert(std::pair<int, int>(0, 1));
    
    mapDM mapTwo;
    mapTwo.insert(std::pair<double, mapIntInt>(8.9, mapOne));
    
    // Now I want to search mapOne, but first I need to search mapTwo for mapOne.
    
    if (twoIterator != mapTwo.end())
    {
       oneIterator = twoIterator->second.find(8.9);
    
       // How do you check if oneIterator points to a valid element?  
    }
    Given the example above, how do you know what if the search in the second map container (map inside of map) points to a valid element?

    Thanks,
    Kuphryn

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    I have not tested this code for any errors, but this is basically how it could be performed:
    Code:
    typedef std::map<int, int> mapIntInt;
    typedef std::map<double, mapIntInt> mapDM;
    
    mapIntInt mapOne;
    mapOne.insert(std:pair<int, int>(0, 1));
    
    mapDM mapTwo;
    mapTwo.insert(std:pair<double, mapIntInt>(8.9, mapOne));
    
    // loop through the main map from beginning to end
    for (mapDM::const_iterator map2_iter = mapTwo.begin();
         map2_iter != mapTwo.end();
         ++map2_iter)
    {
        // create an instance of the sub map for each loop through the main map
        const mapIntInt &map_one(*map2_iter).second);
    
        // assign an iterator to the value looked for
        const mapIntInt::const_iterator map1_iter = map_one.find(8.9);
    
        // if the iterator is not pointing at the end of the map, you found it
        if (map1_iter != map_one.end())
        {
            // found it!
        }
    }
    Hope that helps.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Your solution works!

    // map1_iter != map_one.end()

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. stl - searching for elements in containers
    By Raven Arkadon in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2005, 11:10 AM
  5. STL multimaps / searching
    By Codulation in forum C++ Programming
    Replies: 7
    Last Post: 01-05-2004, 06:28 PM