TotalInsanity
Programmer
Hello All,
I could do with some more expert help so I have come here to seek some out!
Can someone help me with this multimap problem in c++.
I am wanting to create a multimap using two keys and a data value. I understand that they normally use 1 key and a data value as a pair, but I need two keys.
I have created a class here to use two keys:
I have then created my map definition from the STL as follows:
The multimap is populated from a text file containing two doubles,which I want to use as keys, hence the class and a string data type.
I have successfully inserted this data using the following:
PlacePointC, PlacePointD are doubles and strPlace is the string of data. This works as it uses the class to populate the multimap with the two keys I want.
The trouble I am having is when I try to use find the find a specific set of keys.
I am using:
This is creating an interator and calling find on the multimap passing the two keys I want to find using my class. The two double keys are values of type double (dLatitude, dLongitude).
It does a lookup on the map but then displays the wrong data found when I try to cout it to the console.
Can anyone help to explain to me what is wrong here????
If I try to do a find using dLatitude as '52.15' and dLaongitude as '4.15' the string associated with this is 'Aberaeron' but instead of finding this 'double' key and displaying aberaeron it shows me another one at a totally different keys location in the map??
PLEASE PLEASE HELP ME, IT's DRIVING ME NUTS!!
I could do with some more expert help so I have come here to seek some out!
Can someone help me with this multimap problem in c++.
I am wanting to create a multimap using two keys and a data value. I understand that they normally use 1 key and a data value as a pair, but I need two keys.
I have created a class here to use two keys:
Code:
class Keys {
public:
Keys(double k1, double k2) : key1(k1), key2(k2) { }
bool operator<(const Keys &right)
const
{
return (key1 < right.key1 && key2 < right.key2);
}
double key1;
double key2;
};
I have then created my map definition from the STL as follows:
Code:
typedef multimap<Keys, string> locationMultimap;
locationMultimap TestMap;
The multimap is populated from a text file containing two doubles,which I want to use as keys, hence the class and a string data type.
I have successfully inserted this data using the following:
Code:
TestMap.insert(pair<Keys,string>(Keys(PlacePointC, PlacePointD), strPlace));
PlacePointC, PlacePointD are doubles and strPlace is the string of data. This works as it uses the class to populate the multimap with the two keys I want.
The trouble I am having is when I try to use find the find a specific set of keys.
I am using:
Code:
locationMultimap::iterator post = TestMap.find(Keys(dLatitude,dLongitude));
This is creating an interator and calling find on the multimap passing the two keys I want to find using my class. The two double keys are values of type double (dLatitude, dLongitude).
It does a lookup on the map but then displays the wrong data found when I try to cout it to the console.
Can anyone help to explain to me what is wrong here????
If I try to do a find using dLatitude as '52.15' and dLaongitude as '4.15' the string associated with this is 'Aberaeron' but instead of finding this 'double' key and displaying aberaeron it shows me another one at a totally different keys location in the map??
PLEASE PLEASE HELP ME, IT's DRIVING ME NUTS!!