|
-
December 3rd, 2004, 11:31 AM
#1
std::map max/min
Is there a call to get these values?
Thx.
-
December 3rd, 2004, 12:01 PM
#2
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;
}
-
December 3rd, 2004, 01:07 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|