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
VectorMap:how to replace a Value [message #29127] Wed, 06 October 2010 16:17 Go to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Hi,

In a VectorMap, is there any way to replace a Value maintaining its Key? Is the only way: Remove the element (by the Key) and to Add it again with the same Key but with the new Value?

Thank you.

Cheers,

Javier
Re: VectorMap:how to replace a Value [message #29128 is a reply to message #29127] Wed, 06 October 2010 16:45 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Or just simple assignment indexed by key or index?

[Updated on: Wed, 06 October 2010 16:45]

Report message to a moderator

Re: VectorMap:how to replace a Value [message #29130 is a reply to message #29128] Wed, 06 October 2010 19:05 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Would you mind writing the VectorMap method/function you are mentioning?

Thank you.

Javier
Re: VectorMap:how to replace a Value [message #29131 is a reply to message #29130] Wed, 06 October 2010 19:54 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Javier,

I believe cbporter meant this:
	VectorMap<String,int> map
	map.Get("key")=2;
	map[map.Find("key")]=3;

Sorry for giving you such short answer as well, but those are quite trivial and there is not much to say Smile

Honza
Re: VectorMap:how to replace a Value [message #29133 is a reply to message #29131] Wed, 06 October 2010 20:05 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Excellent.

Many thanks to you both.

Javier
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 next message
rxantos is currently offline  rxantos
Messages: 72
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);



Re: VectorMap:how to replace a Value [message #37203 is a reply to message #37201] Wed, 05 September 2012 10:02 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
map.GetAdd("key") = 3;



regards
omari.
Re: VectorMap:how to replace a Value [message #37204 is a reply to message #37201] Wed, 05 September 2012 10:05 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi rxantos

Such methods already exist Wink

Look at FindAdd and GetAdd in the reference.

Best regards,
Honza

PS: Ahh, omari was faster Smile

[Updated on: Wed, 05 September 2012 10:06]

Report message to a moderator

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


Current Time: Fri Apr 19 11:08:10 CEST 2024

Total time taken to generate the page: 0.04629 seconds