Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 784 Bytes

unordered_map.md

File metadata and controls

42 lines (35 loc) · 784 Bytes

Применяем контейнер std::unordered_map для пользовательских типов

#include <iostream>
#include <unordered_map>

struct coord{
    int x;
    int y;
};

bool operator ==(const coord &l, const coord &r)
{
    return l.x==r.x && l.y==r.y;
}

namespace std
{
template<>
struct hash<coord>
{
    using argument_type = coord;
    using result_type = size_t;
    result_type operator()(const argument_type &c) const
    {
        return static_cast<result_type>(c.x) + static_cast<result_type>(c.y);
    }
};

}

int main(){
    std::unordered_map<coord, int> mp {
        {{0,0}, 1},
        {{0,1}, 2},
        {{2,3}, 3},
    };
    for(const auto &[key, value] : mp)
        std::cout << key.x << " "<< key.y<<" "<<value << "\n";
}