The point of the operator of the map is that it creates an entry if it doesn't exist.
In your case: If either Name or next does not exist in your map a new entry with a NULL pointer to STATSSTRUCT is created. You program will crash when you try to access this invalid pointer ->LastError
only thing i can maybe think is that my map hasnt set [Name][next] yet in that case how do i check if its set?
// Check if Stats[Name] exists
auto it = Stats.find(Name);
if (it != Stats.end())
// Check if Stats[Name][next] exists
auto it2 = it->second.find(next);
if (it2 != it->second.end())
// No point in checking if LastError is empty first.
i said i tried the above code an i tried it with the find function as well.
i think i now have it kind of working but i have another function that sets the data.
i was calling it different before an it worked like this
Stats[Name][next].LastError = "test error"
but when i changed it to pointer an used it like this
it crashed with access violation
Fair enough. Use it as a learning exercise. You will notice that pointers doesn't make things "neater" and that they should not be used unless necessary because it complicates things and increases the risk of bugs.
So now if you use pointers you need to make sure that Stats[Name][next] points to a STATSSTRUCT object before you can do
Stats[Name][next]->LastError = "test error"
One way is to allocate an object using new.
Stats[Name][next] = new STATSSTRUCT;
And then you should also remember to delete the object when you are done using it.
when is the best time to use pointers in this kind of thing im doing?
Principally, for when you want to use dynamically allocated memory on the heap, rather than having your data in local variables being copied around.
Also, using pointers can ensure that all code that accesses or changes the contents of these structures is using the same memory, rather than using separate copies of the data. When you put a struct into your map, it's actually creating a new copy of that struct that gets stored in the map, separate from the one in the calling code.