Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » VectorMap:how to replace a Value
Re: VectorMap:how to replace a Value [message #37201 is a reply to message #29131] Wed, 05 September 2012 09:26 Go to previous messageGo to previous message
rxantos is currently offline  rxantos
Messages: 73
Registered: October 2011
Member
Sorry for the thread necromancy. But both
map.Get("key")=2;
map[map.Find("key")]=3;


Gives an assertion fail when the key is not in the map.

Tried with Add but it will just add another copy instead of replacing the value.

Granted, some times one wants to keep the extra copies. But sometimes is just a waste of memory and cycles.

This, however, seems to work:
int i = map.Find("key");
if(0 > i) {
  map.Add("key",2);
} else {
  map[i] = 2;
}


Maybe a Set Function should be added to AMap

T& Set(const K& k, const T& x) {
  int i = Find(k);
  if(0 > i) {
    return Add(k,x);
  } else {
    value[i] = x;
  }
  return value[i];
}


so that the code becomes:
map.Set("key",2);



 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Trouble with Socket
Next Topic: Writes to freed blocks detected
Goto Forum:
  


Current Time: Tue Jul 08 02:35:00 CEST 2025

Total time taken to generate the page: 0.03618 seconds