Link to home
Start Free TrialLog in
Avatar of vesel
vesel

asked on

stl:map

Hi Folks! I am trying to overwrite the values in an stl:map if certain cretria matches. Can anyone show how to do that??
I have a map<string,time_t>
if the key matches with a given constant then I replace the value of that key with new value.
Suppose the map contains
aol   120102
msft  548710
......
if I iterate through the map and see that aol is there then check its value if its >1000 then I change aol's value to 2000.
If there is no aol then I insert aol with a value of 1000.
TIA.
Please reply ASAP
Avatar of vesel
vesel

ASKER

Edited text of question.
Avatar of vesel

ASKER

Edited text of question.
Homework?
can't you just do:

typedef map<string,time_t>::iterator LI

for (LI i=somemap.begin(); i!=somemap.end(); i++)
{
    if (somemap[key]==sometime)
        somemap[key]=newtime;
}

isn't this in your reference book?

jw
void foo(map<string,int>& m, int oldVal, int newVal, char*s key)
{
  map<string,int>::iterator p = find(key);
  if (p!=m.end()) {             // key was found
    if (p->second > oldVal)
        p->second = newVal;
  }
  else {                       // not found  
    m.insert(make_pair(key, newVal));
  }
}
Avatar of vesel

ASKER

I asked a question not to bitch at me!!!
Thx for the folks who answered it....
Arrika Please post your comment with answer I will give you the points.

Thx
ASKER CERTIFIED SOLUTION
Avatar of arikka
arikka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vesel

ASKER

Thx for that,Arrika!this is a part of a big module I was just missing tiny details. No its not a HW I am way to old for all that.