mercredi 24 mars 2021

c++ : unordered map with pair of string_viewes

Here is a code snippet I have :

    struct PairHasher {
        size_t operator()(const std::pair<std::string_view, std::string_view>& stop_stop) const {
            return hasher(stop_stop.first) + 37*hasher(stop_stop.second);
        }
        std::hash<std::string_view> hasher;
    };


BOOST_FIXTURE_TEST_CASE(unordered_map_string_view_pair_must_be_ok, TestCaseStartStopMessager)
{
    const std::vector<std::string> from_stops = {"from_0", "from_1", "from_2"};
    const std::vector<std::string> to_stops = {"to_0", "to_1", "to_2"};

    std::unordered_map<std::pair<std::string_view, std::string_view>, std::int32_t, TransportCatalogue::PairHasher> distance_between_stops;
    for ( std::size_t idx = 0; idx < from_stops.size(); ++idx) {
        std::cout << from_stops[idx] << " : " << to_stops[idx] << std::endl;
        distance_between_stops[std::pair(from_stops[idx], to_stops[idx])] = idx;
    }

    std::cout << "MAP CONTENT :" << std::endl;
    for (auto const& x : distance_between_stops)
    {
        std::cout << x.first.first << " : " << x.first.second << std::endl;
    }
}

I expect to see 3 pairs inside the container, but there is only 1 concerning to the output :

MAP CONTENT :
from_2 : to_2

So, where are two more pair lost? What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire