C++ unordered_set of vectors
Asked Answered
A

2

46

Can I create a unordered_set of vectors in C++? something like this

std::unordered_set<std::vector<int>> s1;

because I know that is possible with the "set" class of the std lib but seems that it doesn't work for the unordered version thanks

Update: this is the exactly code that I'm trying to use

typedef int CustomerId;
typedef std::vector<CustomerId> Route;
typedef std::unordered_set<Route> Plan;

// ... in the main
Route r1 = { 4, 5, 2, 10 };
Route r2 = { 1, 3, 8 , 6 };
Route r3 = { 9, 7 };
Plan p = { r1, r2 };

and it's all right if I use set, but I receive a compilation error when try to use the unordered version

main.cpp:46:11: error: non-aggregate type 'Route' (aka 'vector<CustomerId>') cannot be initialized with an initializer list
    Route r3 = { 9, 7 };
Afflictive answered 24/4, 2015 at 19:30 Comment(8)
Did you mean something like std::unordered_set<std::vector<int>> actually?Ddene
I'm sorry, I have digit the incorrect class name, I mean exactly unordered_setAfflictive
How doesn't it work? What's the problem you're having?Clevie
What does doesn't work mean? Please post an SSCCEKlarrisa
Yes you can have an std::unordered_set<std::vector<some_type>>Shaduf
I've updated the post to clarify the errorAfflictive
@CattaniSimone Uh... what please? What compiler are you using that r3 fails but not the Plan constructor?Ahl
@Ahl - happens with me too when using clang.Tourney
A
57

Sure you can. You'll have to come up with a hash though, since the default one (std::hash<std::vector<int>>) will not be implemented. For example, based on this answer, we can build:

struct VectorHash {
    size_t operator()(const std::vector<int>& v) const {
        std::hash<int> hasher;
        size_t seed = 0;
        for (int i : v) {
            seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }
        return seed;
    }
};

And then:

using MySet = std::unordered_set<std::vector<int>, VectorHash>;

You could also, if you so choose, instead add a specialization to std::hash<T> for this type (note this could be undefined behavior with std::vector<int>, but is definitely okay with a user-defined type):

namespace std {
    template <>
    struct hash<std::vector<int>> {
        size_t operator()(const vector<int>& v) const {
            // same thing
        }
    };
}

using MySet = std::unordered_set<std::vector<int>>;
Ahl answered 24/4, 2015 at 19:33 Comment(5)
The second half is probably UB. As far as the library is concerned, I don't think std::vector<int> counts as a user-defined type.Corrupt
Why is the default one (std::hash<std::vector<int>>) not used?Rog
@Rog There is no std::hash<std::vector<int>> - it's not provided by the standard library.Ahl
How does std::set<vector<int>> work then if there is no default hash?Camber
@Camber std::set isn't a hash table, it's a binary tree. It uses <Ahl
C
2

As an alternative to a custom-written hasher, Boost provides a hasher for many standard library types. This should work in your case:

#include <boost/container_hash/hash.hpp>

std::unordered_set<
  std::vector<int>,
  boost::hash<std::vector<int>>
> s1;

Reference: https://www.boost.org/doc/libs/1_78_0/doc/html/hash/reference.html

In older Boost versions, the header file was boost/functional/hash.hpp.

Conventional answered 8/2, 2022 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.