#include <unordered_map>
#include <string>
#include <string_view>
#include <memory>
struct Foo
{
int value;
float data;
};
struct TransparentHash
{
using is_transparent = void; // lol, ok?
size_t operator()(std::string_view sv) const noexcept
{
return std::hash<std::string_view>{}(sv);
}
};
struct TransparentEqual // written by ai btw
{
using is_transparent = void;
bool operator()(std::string_view a, std::string_view b) const noexcept
{
return a == b;
}
};
int main()
{
std::unordered_map<std::string, std::unique_ptr<Foo>, TransparentHash, TransparentEqual> map;
map.emplace("hello", std::make_unique<Foo>(42, 3.14f));
std::string key = "hello";
std::string_view sv = key;
auto it = map.find(sv);
}
So apparently, this is how you pass a std::string_view as the key to a map?
So judging by the amount of nonsense to do it, this can't be useful right? Like I shouldn't bother to key the container with a std::string_view and instead just use a std::string right?
Because I have to believe that if this was a sane thing to do, then the STL would allow passing the std::string_view without this other stuff?
I mean I did this just because I thought that std::string_view is generally preferred over std::string where its possible to do so. So why might it not be in this case?
