Home » U++ Library support » U++ Core » VectorMap.Get (Problem fetching value of vectormap second time)
VectorMap.Get [message #45231] |
Tue, 13 October 2015 14:31  |
 |
deep
Messages: 268 Registered: July 2011 Location: Bangalore
|
Experienced Member |
|
|
Hi,
When I want to get the content of VecotorMap second time I am getting Assert Failed error.
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
Vector<int> v1;
VectorMap<String,Vector<int>> Vm ;
v1.Clear();
v1.Add(0);
v1.Add(3);
Vm.Add("key1",v1);
v1.Clear();
v1.Add(10);
v1.Add(31);
v1.Add(41);
Vm.Add("key2",v1);
Vector<int> v2 ;
v2 = Vm.Get("key1");
DUMP(v2);
v2.Clear();
v2 = Vm.Get("key2");
DUMP(v2);
v2.Clear();
v2 = Vm.Get("key1");
DUMP(v2);
}
Log content
v2 = [0, 3]
v2 = [10, 31, 41]
v2 = ****************** ASSERT FAILED: Assertion failed in c:\devtools\upp_git\uppsrc\core\Vcont.h, line 34
Broken rval_ semantics
It is working whith VectorMap<String,String>
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
VectorMap<String,String> v1;
v1.Add("key1","val1");
v1.Add("key2","val2");
v1.Add("key3","val3");
String s;
s.Clear();
s= v1.Get("key1");
DUMP(s);
s.Clear();
s= v1.Get("key2");
DUMP(s);
s.Clear();
s= v1.Get("key3");
DUMP(s);
s.Clear();
s= v1.Get("key1");
DUMP(s);
}
Log content
s = val1
s = val2
s = val3
s = val1
Warm Regards
Deepak
[Updated on: Tue, 13 October 2015 14:42] Report message to a moderator
|
|
|
Re: VectorMap.Get [message #45232 is a reply to message #45231] |
Tue, 13 October 2015 16:38   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello Deep,
Try to clone your vector. I think in your case there is move operation for some reason. Maybe someone that knows better that mechanism can describe it for you. On the other hand I have got compilation error of your example in the latest upp. Code that should work:
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
Vector<int> v1;
VectorMap<String, Vector<int>> Vm;
v1.Clear();
v1.Add(0);
v1.Add(3);
Vm.Add("key1", v1); // <- Vector was copied
v1.Clear();
v1.Add(10);
v1.Add(31);
v1.Add(41);
Vm.Add("key2", v1); // <- Vector was copied
Vector<int> v2;
v2 = clone(Vm.Get("key1")); // <- Without clone, probably move operation
DUMP(v2);
v2.Clear();
v2 = clone(Vm.Get("key2"));
DUMP(v2);
v2.Clear();
v2 = clone(Vm.Get("key1"));
DUMP(v2);
}
Here is my log values:
v2 = [0, 3]
v2 = [10, 31, 41]
v2 = [0, 3]
------------------------------------------------------------ --
P.S.
Maybe you should put space after comma. In my opinion declaration like: VectorMap<String, Vector<int>> is more readable that VectorMap<String,Vector<int>> .
Sincerely,
Klugier
U++ - one framework to rule them all.
[Updated on: Tue, 13 October 2015 16:47] Report message to a moderator
|
|
|
|
Re: VectorMap.Get [message #45239 is a reply to message #45231] |
Thu, 15 October 2015 12:01  |
 |
deep
Messages: 268 Registered: July 2011 Location: Bangalore
|
Experienced Member |
|
|
Hi Mirek and Klugier,
Thanks for response.
Both working.
Will use
const Vector<int>& v2 = Vm.Get("key1");
Need to only use the returned values.
Warm Regards
Deepak
|
|
|
Goto Forum:
Current Time: Tue May 13 07:56:30 CEST 2025
Total time taken to generate the page: 0.00952 seconds
|