CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Posts
    116

    std::map max/min

    Is there a call to get these values?
    Thx.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: std::map max/min

    Could you explain what you want in a little more detail ?

    begin() returns an iterator for the first element (which would be the
    "min" for the key) . end() points to last element , so subtracting 1
    points to last element.

    Code:
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    typedef std::map<int,int>  MAP;
    typedef std::pair<int,int> PAIR;
    
    int main()
    {
    
        MAP squares_map;
    
        squares_map.insert( PAIR(2,4) );
        squares_map.insert( PAIR(0,0) );
        squares_map.insert( PAIR(1,1) );
        squares_map.insert( PAIR(6,36) );
        squares_map.insert( PAIR(3,9) );
    
        if (!squares_map.empty())
        {
            MAP::iterator it_min = squares_map.begin();
    
            MAP::iterator it_max = squares_map.end();  // points to just past last element
            --it_max;                                  // points to last element
    
            cout << "min element : " << it_min->first << " " << it_min->second << "\n";
            cout << "max element : " << it_max->first << " " << it_max->second << "\n";
        }
    
        return 0;
    }

  3. #3
    Join Date
    Jan 2004
    Posts
    116

    Re: std::map max/min

    Ok. Thx Philip, that is what I wanted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured