ddz
ddz

Reputation: 526

Adding elements into a c++ map

I have a map in my program that stores the code of a Product p and the amount of it.

If a ask for a new request, and if the product already exists in the map, i just need to sum the second element of the pair(code, amount) to the element in the map.

How can I do this?

void Request :: addItem (Product p, double amount) {
    if(this->isItemRequest(p)) {
        //p already exists in the map.
    }

    this->rdata.insert(pair<int, double>((int)p.getCode(), amount));
}

Thanks a lot!

Upvotes: 1

Views: 4603

Answers (4)

Joel
Joel

Reputation: 2035

Easy: map.find :)

#include <iostream>
#include <map>
#include <string>

typedef std::map<std::string, int> StringIntMap;

int main() {
    StringIntMap map;
    map["coke"] = 10;
map["fries"] = 25;
    map["pizza"] = 50;
std::cout << "Before increase" << std::endl;
StringIntMap::const_iterator it;
for (it = map.begin(); it != map.end(); it++) {
    std::cout << it->first << ": " << it->second << std::endl;
}
std::cout << "Now, increase pizza +50" << std::endl;
StringIntMap::iterator item = map.find("pizza");
if (item != map.end()) {
    // pizza exists increase 50
    item->second += 50;
} else {
    std::cout << "Sorry, no pizza here" << std::endl;
}
std::cout << "after increase" << std::endl;
for (it = map.begin(); it != map.end(); it++) {
    std::cout << it->first << ": " << it->second << std::endl;
}
return 0;

}

Upvotes: 0

Sergey K.
Sergey K.

Reputation: 25386

Assuming your map is declared within the Request class as std::map<int, double> rdata, the code can be:

void Request::addItem( Product p, double amount )
{
    if ( this->isItemRequest(p) )
    {
        int Code = int(p.getCode);
        this->rdata[ Code ] += amount;
    }

    this->rdata.insert( pair<int, double>(int(p.getCode), amount) );
}

However, if isItemRequest() is just a trivial check, your code can be simplified to just:

void Request::addItem( Product p, double amount )
{
    int Code = int(p.getCode);
    this->rdata[ Code ] += amount;
}

P.S. Maybe, it is a good idea (if you can change the interface) to pass Product by const reference.

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254471

The simplest way to do that is

rdata[p.getCode()] += amount;

If it wasn't already in the map, then [] will insert an element with value zero, so the value will end up being amount. If it was, then amount is added to the existing value.

Upvotes: 1

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

If you call on:

this->rdata[key] = value;

you create a value using default constructor (int() initializes to 0), return reference to it, and call operator= on it. You can avoid that by checking if key exist first:

this->rdata.count(key) != 0

or simplier

this->rdata.count(key)

if it exists you can use operatror=, operator+= and so on on reference returned by operator[]:

if (this->rdata.count(key) == 0)
    this->rdata.insert( pair<int, double>( key, value ) );
else
    this->rdata[key] += value;

but in this simple case

this->rdata[key] += value;

should just do.

Upvotes: 2

Related Questions