I have a structure like
struct category {
int id,newID;
std::string name;
};
and a map like
std::map<std::string, category> categories;
is it safe to insert details into the map like
category x = {1,2,"Name"};
category y = {2,3,"Mike"};
categories["FIRST"]=x;
categories["SECOND"]=y;
or should i declare the map like
std::map<std::string, category*> categories;
and make structure pointers and insert it? which is safer?
What do you mean by safer? Both methods are just fine. When using the second way you must make sure to delete your category objects. On the first one they are deleted automatically when you delete your map.
The first one is slower if you need to pass the objects a lot around since you will be making copies. The second one would just copy pointers.
So it’s up to you to decide which one fits your needs.