`std :: unordered_map` without duplicating key data

I have a class Personthat has property name( std::string).

I want to create a lookup table, a std::unordered_map, so I can find Personby their name. However, given a Person, I also want to be able to get their name.

This requires saving nametwice - once as a card key and once inside the person object, as shown in my code below.

Since I have many Personloaded into memory right away, I don’t want the overhead of storing their names twice.

I tried using key references / pointers instead in the class Person, but this creates problems, since the map seems to shuffle its data when it changes, and the links become invalid.

I also tried using std::unordered_set, but that means I need to kiss the object Personevery time I want to do a search.

Is there a way to use the same data for the key and value of an unordered card?

#include <iostream>
#include <unordered_map>


class Person
{
    private:
        const std::string _name;

    public:
        Person( const std::string& name ) : _name( name )
        {
        }


        const std::string& get_name() const
        {
            return _name;
        }
};


int main()
{
    auto my_set = std::unordered_map<std::string, std::shared_ptr<Person>>();

    my_set.insert( { "alice", std::shared_ptr<Person>( new Person( "alice" )) } );
    my_set.insert( { "bob", std::shared_ptr<Person>( new Person( "bob" )) } );
    my_set.insert( { "charlie", std::shared_ptr<Person>( new Person( "charlie" )) } );

    std::cout << my_set.find( "bob" )->second->get_name() << std::endl;

    return 0;
}
+6
source share
4 answers

With std::setyou can use a transparent resolver ( std::unordered_setdoesn't seem to support this: /):

struct LessPerson
{
    using is_transparent = void; // enable "transparent" comparer

    template <typename T1, typename T2>
    bool operator ()(const T1& t1, const T2& t2) const
    {
        // Compare only "name".
        return toString(t1) < toString(t2);
    }

    // trivial one
    const std::string& toString(const std::string& s) const
    {
        return s;
    }

    // the one why we create the class
    const std::string& toString(const Person& p) const
    {
        return p.get_name();
    }

    // A tricky one to handle dereference of (smart) pointers.
    template <typename T,
              std::enable_if_t<std::is_same<Person, std::decay_t<decltype(*std::declval<T>())>>::value>* = nullptr>
    const std::string& toString(const T& p) const
    {
        return (*p).get_name();
    }

};

And then use it:

auto my_set = std::set<std::shared_ptr<Person>, LessPerson>();

my_set.insert( { std::make_shared<Person>("alice") } );
my_set.insert( { std::make_shared<Person>("bob") } );
my_set.insert( { std::make_shared<Person>("charlie") } );

auto it = my_set.find("bob"); // search using "bob" directly without creating a new Person

Demo

+2
source

Boost.Multi-index . , . , :

namespace mpi = boost::multi_index;
boost::multi_index_container<
        Person,
        mpi::indexed_by<
           mpi::hashed_unique< mpi::const_mem_fun< Person, const std::string &, &Person::get_name > >
        >
> my_set;

:

auto f = my_set.find( "bob" );
if( f != my_set.end() )
    std::cout << f->get_name() << std::endl; 

, , Person , . , , ( const std::string &get_phone() const):

boost::multi_index_container<
        Person,
        mpi::indexed_by<
           mpi::hashed_unique< mpi::const_mem_fun< Person, const std::string &, &Person::get_name >,
           mpi::hashed_unique< mpi::const_mem_fun< Person, const std::string &, &Person::get_phone >>
        >
> my_set;

// lookup by phone:

const auto &idx = boost::get<1>( my_set );
auto f = idx.find( "1234567890" );
if( f != my_set.end() )
    std::cout << f->get_name() << std::endl; 

: , , , , .

+2

"" , , string string . hash equal .

struct myhash
{
    unsigned operator()(std::string* s) const
    {
        return std::hash<std::string>()(*s);
    }
};

struct myequal
{
    unsigned operator()(std::string* s1, std::string* s2) const
    {
        return *s1 == *s2;
    }
};
...
auto my_set = std::unordered_map<std::string*, std::shared_ptr<Person>, myhash, myequal>();

: string.

std::string b = "bob";
std::cout << my_set.find(&b)->second->get_name() << std::endl;

bob inline, .

+1

, boost::flat_set. , , , . , unordered_, .

, unordered_map, , unordered_multiset, , , , . , , .

, - :

#include <string>
#include <iostream>
#include <unordered_map>

class Person {

public:
    Person(const std::string& name, const int age) : name_(name), age_(age) {}
public:
    const std::string& name() const { return name_; }
    int age() const { return age_; }
private:
    std::string name_;
    int age_;
};

int main()
{
    Person p1("Joe", 11), p2("Jane", 22), p3("James", 33), p4("Joe", 44);
    std::unordered_multimap<size_t, Person> persons{ {std::hash<std::string>()(p1.name()), p1}, {std::hash<std::string>()(p2.name()), p2},{std::hash<std::string>()(p3.name()), p3}, {std::hash<std::string>()(p4.name()), p4} };
    auto potential_joes = persons.equal_range(std::hash<std::string>()("Joe"));
    for (auto it = potential_joes.first; it != potential_joes.second; ++it) {
        if (it->second.name() == "Joe") {
            std::cout << it->second.name() << " is " << it->second.age() << " years old" << std::endl;
        }
    }
}

, , , . , unordred_map , .

, , . , , , , , name_ const , const.

0

All Articles