I have an object that can be identified by name , and I want to put it in one of the STL containers .
class MyClass {
public:
private:
std::string name;
};
So, at first I thought that using structures similar to a map does not matter in my case, because in these structures the identifier (key) is separated from the class itself. Using the card, I must return the name variable and copy it outside the class (memory loss and illogicality, violation of OOP rules).
My next snapshot was using structure-bound structures . In this case, I only have a key field where I load my entire object. Using this method, I have to overload my <, โ and == operators to use the object as a key. I can even create a hash functor if I use unordered_set, it works fine. The problem here is that I cannot use the functions of the container , as with the map. It is valid mapInstance.find("example"), and it is not setInstance.find("example"). I have to create an object with the member variable nameset to "example" and pass it to the function find(). The problem with this solution is that the other member variables in my class are duplicated and not used. I even tried to overload the <,> and == operators for classes std::stringand MyClassthat work fine if I use them like this stringInstance < MyClassInstance, but the container functions are unsuitable (I even tried to overload the functor to work with the string without success).
Can you offer me a simple way (or way) how to solve this problem using std::setor std::map(possibly others)? The std::mapkey cannot be a link (as I know), and I do not know how to resolve it using std::set.
. map , unordered_map map, , (- , ).
!