Try
it++
?
This thread encouraged me to try out a multi-map, so forgive me for some newb stuff. The multi-map is basically a binary tree which has its contents sorted via the key. Therefor, the fast searching time of your map is wasted when you search via the value.
If you can ensure that your keys are always unique, then erasing a node in the map is as simple as:
myMap.erase(myMap.find(keyValue);
If your key values are not unique, then that code will only erase the first pair that it finds and doesn't work the way you'd want. You need to find the range of said key, and then search through that range looking for your value (if you want to follow my lead).
I've been playing with a file from a game, and thought I'd play with multi-maps with this file:
btw, the incrementing works fine on my computer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
int main(void)
{
// Techs is a class holding (string, int, string)
multimap<int, Tech> techs;
Tech *hold;
Tech temp;
ifstream input("input.txt");
while (temp.read(input).good()) // Reads the line of the file
{
techs.insert(pair<int, Tech>(temp.cost, temp));
// This is to simulate your "CEntityManager* obj"
if (temp.name == "FUSION")
hold = new Tech(temp.name, temp.cost, temp.era);
}
input.close();
// The multi-map key's are the cost of the tech
// These two lines create a range with these key values
pair<multimap<int,Tech>::iterator,multimap<int,Tech>::iterator> range;
range = techs.equal_range(8000); // "FUSION" happens to have a cost of 8000
// Find the "CEntityManager* obj"
multimap<int, Tech>::iterator iter;
for (iter = range.first; iter != range.second; iter++)
{
if (iter->second == *hold) break;
}
// Erase it
techs.erase(iter);
//Looks good to me
for (iter = techs.begin() ; iter != techs.end() ; iter++)
{
iter->second.print();
}
delete hold;
cin.get();
return 0;
}
|
So I would imagine you could use your "name" variable to find the range, and then look through that range with your "*obj"