I am reading in a file and I am trying to make it to where if I find an object already in the map, it will add that value of the new object to the object already found in the map together. What I don't know is how to do this with the correct syntax with maps. Here's what I have:
struct ap_pair {
ap_pair(float tp, float tm) : total_price(tp), total_amount(tm) {};
ap_pair & operator+=(const ap_pair &);
float total_price;
float total_amount;
};
void APC :: compute_total ()
{
string name;
map<string, ap_pair> :: iterator my_it;
float num1, num2, num3;
while (!fs.eof() )
{
fs >> name >> num1 >> num2; //read in file
ap_pair myobj(num1, num2); //send the weight/count and per unit price ap_pair
my_it = mymap.find(name); //returns iterator
if (my_it != mymap.end())
{
// myobj+= //ERROR here. how can I add the new object to the object already in the map?
}
else
mymap.insert(pair<string, ap_pair>(name, myobj));
if (fs.eof()) break; //makes it so the last line is not repeated
num3= num1*num2;
total_amount+=num1;
total_price+= num3;
}
}
I am going through an iterator with the if condition. it should find a match with the same names, but how can I add the values of the object found with the object in the map already?