Using a pair as a key for a map -- can't find the right method ANYWHERE

I'm using namespace std; and I declare the following:

map<pair<double,int>, double> mymap;

and I insert values into my map like so:

mymap[make_pair(x,y)] = specificValue;

I want to be able to do the following:

Check if mymap[x][y] exists, and if so, return corresponding specificValue. If not, insert the specificValue under x,y in the map.

Seems fairly straightforward but I've spent the past two hours scouring the net with little luck. Thanks!
Last edited on
Any type can work as a key in map as long as it has the proper operator overloads. IIRC, you need to have == and < overloaded. I don't believe std::pair has these overloaded, so if you want to use it as a key you'll have to make your own overloads.

HOWEVER, you should not be using a floating point for any part of a key in any map -- for the same reason you shouldn't use them with == and != operators. Since floating points are approximations, they are unreliable as keys.

Therefore pair<double,int> will fail horribly as a key, even if you got it to compile.
Make a nested for loop and make each entry '\0' ( NULL )

Then to get the values:
1
2
3
4
5
6
for( int i = 0; i < topValueX; ++i )
    for( int j = 0; j < topValueY; ++j )
        if( myMap[ i ][ j ] )
            std::cout << myMap[ i ][ j ] << '\n';
        else
            std::cout << "Non-existant value." << '\n';


The if statement will return false when you hit a NULL value.

Hope this helps.
To be able to write mymap[x][y] you have to create a map that maps to a map. map<double, map<int,double>> mymap; This will probably be less efficient but it does what you want.
technically my key doubles are whole numbers, but I use them only because I use them in pow() functions later and they give me all sorts of trouble when I use ints. Doubles work, no headache, that's all I care about right now.

But is a double for loop the best way to check for the existence of x and y?
Double for loop? No that sounds like a very bad idea. In Lynx876 I assume myMap is a 2D array and not an std::map which only make sense if the key values are not far in between. The for loops is used to print all values in the map.
I've never used maps, not sure how they work. But the question was written:
Check if mymap[x][y] exists, and if so, return corresponding specificValue. If not, insert the specificValue under x,y in the map.


So would I be right in saying that the "check", to see if a value is existant if( myMap[ i ][ j ] ), assuming they are all NULL to begin with, then entering values, work?

If so, you can then use that if statement to input something in to the index you are on.
Last edited on
I don't know if it HAS to be mymap[x][y] specifically, as I am somewhat new-ish to C++

All I want is to toss in x and y to mymap somehow and have it kick back specificValue if the combination x and y exist together as the keys, and if not, insert specificValue.
Last edited on
1
2
int x = ...;
pow( static_cast<double>(x), ... );
With a map, operator[] will always return a value. If it's the first time you're used a key, the value will be the default.
http://www.cplusplus.com/reference/stl/map/operator%5B%5D/

You need to use find() to test for existence.
http://www.cplusplus.com/reference/stl/map/find/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
map<pair<int,int>, double> mymap;

...

mymap[make_pair(x,y)] = specificValue;

...

// check for existence
if(mymap.find(make_pair(x,y)) != mymap.end())
    ...

map<pair<int,int>, double>::iterator iter = mymap.find(make_pair(x,y));
if(iter != mymap.end())
{
    double value = (*iter).second; // or iter->second;
    ...
}

Last edited on
What is

map<pair<int,int>, double>::iterator iter = mymap.find(make_pair(x,y));
if(iter != mymap.end())
{
double value = (*iter).second; // or iter->second;
...
}

does this do the same thing as doing double value = mymap[make_pair(x,y)]; ?
Not quite.

As I said above, operator[] always returns a value, whatever key you use. So if you only want to find the values you actually set, you have to use the find approach.

For a map that stores doubles, mymap[make_pair(x,y)] will return 0.0 for all values of x,y that do not already have a value, creating new map entries as required.
Last edited on
Topic archived. No new replies allowed.