How to insert into a 2d std::map - c++

I am trying to insert data into a 2d map but the following code gives me a Access violation error.
void StateManager::AddState(string stateName, map<string, IBaseComponent*> stateComponents)
{
// Add the state to the states map
m_States.insert(pair<string, map<string, IBaseComponent*>>(stateName,stateComponents));
}
Can someone point out what I am doing wrong and how I can fix it ?
Edit: I tried inserting some test values into the map and that works fine. Must be a problem with the data I am inserting.

Turns out I was using the function before initializing the object that contains it. So m_States wasn't actually created yet. Hence the access violation error.

Related

Im trying to convert a wxVector<m_Product*> element to a wxArrayString without any result (Exception thrown at...)

The thing is, im trying to transform the data of a class that is the name of the products. That name is into a std::string format and it should be easy to just make a simple for to put it in a wxArrayString right? but the thing is that the vector where is stored the information its like "corrupted" or "private" i'd like to say.
By the way i tried this function made by me:
BUT i do get this message when i try to use that function
ERROR:
Any idea why is this happening? i mean in the previous line i used de m_GlobalProductVector to print its size to the console...

Segmentation Fault when i use GetInternalField() FROM V8 library

i use the newer version of the v8 library. When i run demo from this page(process.cc),
my program crash when GetInternalField is called in UnwrapMap function. Does anyone have a similar problem, or does they know how to solve it?
Here example:
map<string, string>* JsHttpRequestProcessor::UnwrapMap(Local<Object> obj) {
Local<External> field = Local<External>::Cast(obj->GetInternalField(0)); // here segmentation fault
void* ptr = field->Value();
return static_cast<map<string, string>*>(ptr);
}
EDIT:
I solved my problem. I had to insert macro V8_COMPRESS_POINTERS = 1. Thank you
The provided sample works fine here; did you modify it in any way? How exactly are you running it?
You can only call obj->GetInternalField(0) if you know that obj is an object with internal fields. You can use obj->InternalFieldCount() to check. Objects only have internal fields if you, the embedder, gave them any. If you are trying to convert a regular JavaScript object to a C++ map, you'll have to iterate over its properties, not its internal fields.

list package `MoveToFront` not working for me

New to Go and building a simple LRU cache in Go to get used to syntax and Go development.
Having an issue with the MoveToFront list method, it fails on the following check in the MoveToFront body
if e.list != l || l.root.next == e
I want to move the element (e) to the front of the list when I retrieve it from cache , like this
if elem, ok := lc.entries[k]; ok {
lc.list.MoveToFront(elem) // needs fixing
return elem
}
return nil
The Code can be seen here on line 32 the issue occurs
https://github.com/hajjboy95/golrucache/blob/master/lru_cache/lrucache.go#L32
There seem to be two problems, to me. First, this isn't how the List data type is meant to be used: lc.list.PushFront() will create a List.Element and return a pointer to it. That's not fatal, but at the least, it is kind of annoying—the caller has to dig through the returned List.Element when using Get, instead of just getting the value.
Meanwhile, presumably the failure you see is because you remove elements in Put when the LRU-list runs out of space, but you don't remove them from the corresponding map. Hence a later Put of the just-removed key will try to re-use the element in place, even though the element was removed from the list. To fix this, you'll need to hold both key and value. (In my simple experiment I did not see any failures here, but the problem became clear enough.)
I restructured the code somewhat and turned it into a working example on the Go Playground. I make no promises as to suitability, etc.

Vector_push back not populating the vector (c++)

I'm not getting any error messages, simply my vector is not populating. Looking at the vector in the watch list, nothing is being copied. Why is this?
I've tried two ways.
The first
std::vector<Point3D*> hitpoints;
local_hit_point = sr.local_hit_point; //local_hit_point class Point3D
hitpoints.push_back(local_hit_point);
The second way I tried to use pointers
std::vector<Point3D*> hitpoints;
Point3D* hittingpoint_ptr = new Point3D;
local_hit_point = sr.local_hit_point;
hittingpoint_ptr = &local_hit_point;
hitpoints.push_back(hittingpoint_ptr);
I got vectors in other places in my code which work. Am I really just being daft, but I can't seem to figure out why its not working.
My best guess is that you have an issue with you debugger..
First Suggestion;
Clear everything in your watchlist because they can change the behaviour of the execution
check it again..
Second suggestion;
Create a new project and write a simple code like the one above and see whether your vector is populating..If this simple project works, you should provide us more code and details..
simply my vector is not populating.
It is populating. However
Looking at the vector in the watch list ...
I used hitpoint.size()
Results of function/method calls (size() is a method) are not automatically updated in visual studio watch list (because you haven't told what os/compiler you use I had to assume it is visual studio). I.e. if you enter function call into watch list, it'll calculate results, but will not call function again until you manually refresh it. Instead of function call, add vector itself into watch list.

Assertion error in std:vector used in std::set_difference

I am trying to find the set difference of two vectors, so i do something like this:
std::vector<sha1_hash> first_vec, second_vec, difference_vec;
// populate first_vec and second_vec ...
std::sort(first_vec.begin(),first_vec.end());
std::sort(second_vec.begin(),second_vec.end());
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
difference_vec.begin());
When i run this in debug, i get the following run-time assertion failure (in 'vector'):
_SCL_SECURE_VALIDATE_RANGE(_Myptr < ((_Myvec *)(this->_Getmycont()))->_Mylast);
I am using VS 2008.
Any ideas on what can trigger this?
Like most c++ algorithms, set_difference does not create new entries in the output vector where none existed before. You ned to create space in the output to hold the results.
Edit: Or use an an insert iterator (following untested):
back_insert_iterator< std::vector<sha1_hash> > bi( difference_vec );
std::set_difference(first_vec.begin(),first_vec.end(),
second_vec.begin(),second_vec.end(),
bi);