Using STL containers with a class containing a private key

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:
    //getters and setters, other functions
private:
    std::string name;
    //other member variables
};

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, , (- , ).

!

+4
2

, .

:

  • ?

std:: map , . . . . , std:: map .

std:: list std:: find_if . O (n).

+1

, , . , , , .

, . , std::vector, , , .

:

struct Obj
{
   int key;
   std::string name;
};

bool operator<(const Obj& lhs, const Obj& rhs)
{
   return lhs.key < rhs.key;
}

bool operator==(const Obj& lhs, int rhs)
{
   return lhs.key == rhs;
}

bool operator<(const Obj& lhs, int rhs)
{
   return lhs.key < rhs;
}

flat_map, , , (, ). , . , ( ) .

class flat_map
{
public:
   using container_type = std::vector<Obj>;

   // insert object into the set
   // complexity varies based on length of container
   void insert(Obj&& obj)
   {
      container_.emplace_back(std::move(obj));
      std::sort(container_.begin(), container_.end());
   }

   // find with O(log N) complexity    
   container_type::iterator find(int key)
   {
      auto it = std::lower_bound(container_.begin(), container_.end(), key);

      if(it != container_.end() && *it == key)
         return it;

      return container_.end();
   }

private:
   container_type container_;
};

:

int main()
{
   flat_map obj;

   obj.insert({1, "one"});
   obj.insert({2, "two"});
   obj.insert({3, "three"});

   auto it = obj.find(2);

   std::cout << it->key << ' ' << it->name << '\n';
}

flat_map . , templatizing (, , ..).

+1

All Articles