i have an object – Employee, and i want to know how to insert this object to a map structure sorted by char* lastName field. Thanx.
My map need to contain pointers to Employee objects not the objects themselves. the key is the last name of the employee, the map need to be sorted by the employees last name, should i use multimap?
i have an object – Employee, and i want to know how to insert
Share
So you’ve got an std::map with a custom comparator function (you’ve overloaded the less than operator) and you want to insert objects so that they’re in the right order?
where
myKeyis the key to your map. However, from the sound of your question it actually sounds like you’re using the object as it’s own key. In that case, just use std::set andI’d also suggest that you not use
char*as a means to store lastName and prefer the std::string.EDIT:
Following the comments down below, are you having to be able to pass in a
const char*into another function? If so, still usestringas it has a nice method.c_str()specifically for legacy compatibility. It can be used as so:and voila, you’re much safer!