0

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?

1 Answer 1

4

A std::map iterator is a pair. The first element is the key and the second is the value. If you want to add the new object to the found one, you can do so like this:

my_it->second += myobj;

The ->second will give you a reference to the object at that place in the map, and then you just call your defined += operator on it.

Furthermore, if you create a default constructor for your pair type (perhaps it zeros out the two fields), then your code can be simplified to

while (!fs.eof() )
{
    fs >> name >> num1 >> num2; //read in file
    mymap[name] += ap_pair(num1, num2);

    // ... Rest of the loop...
}

If the operator [] doesn't find a value associated with name, it will default-construct one and then perform the addition.

Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic! It looks like it works. If I want to output the values of the container in the map, how can I do that? I can output the names with the for loop: for (my_it =mymap.begin(); my_it != mymap.end(); my_it++) cout << my_it->first<<endl;
You would either need to overload operator << for your ap_pair type, or access the members individually through my_it->second. e.g. cout << my_it->second.total_price. Also, if this answer was satisfactory for you, feel free to click the check mark and "accept" it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.