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
veselAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
arikkaConnect With a Mentor Commented:
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));
  }
}
0
 
veselAuthor Commented:
Edited text of question.
0
 
veselAuthor Commented:
Edited text of question.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
chensuCommented:
Homework?
0
 
jelaiwCommented:
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
0
 
arikkaCommented:
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));
  }
}
0
 
veselAuthor Commented:
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
0
 
veselAuthor Commented:
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.
0
All Courses

From novice to tech pro — start learning today.