std::unordered_set<Key,Hash,KeyEqual,Allocator>::find

1,2) Finds an element with key equivalent to key.

# Declarations

iterator find( const Key& key );

(since C++11)

const_iterator find( const Key& key ) const;

(since C++11)

template< class K >
iterator find( const K& x );

(since C++20)

template< class K >
const_iterator find( const K& x ) const;

(since C++20)

# Parameters

# Return value

An iterator to the requested element. If no such element is found, past-the-end (see end()) iterator is returned.

# Notes

Feature-test macro Value Std Feature __cpp_lib_generic_unordered_lookup 201811L (C++20) Heterogeneous comparison lookup in unordered associative containers; overloads (3,4)

# Example

#include <cstddef>
#include <functional>
#include <iostream>
#include <source_location>
#include <string>
#include <string_view>
#include <unordered_set>
 
using namespace std::literals;
 
namespace logger { bool enabled{false}; }
 
inline void who(const std::source_location sloc = std::source_location::current())
{
    if (logger::enabled)
        std::cout << sloc.function_name() << '\n';
}
 
struct string_hash // C++20's transparent hashing
{
    using hash_type = std::hash<std::string_view>;
    using is_transparent = void;
 
    std::size_t operator()(const char* str) const
    {
        who();
        return hash_type{}(str);
    }
    std::size_t operator()(std::string_view str) const
    {
        who();
        return hash_type{}(str);
    }
    std::size_t operator()(std::string const& str) const
    {
        who();
        return hash_type{}(str);
    }
};
 
int main()
{
    std::unordered_set<int> example{1, 2, -10};
 
    std::cout << "Simple comparison demo:\n" << std::boolalpha;
    if (auto search = example.find(2); search != example.end())
        std::cout << "Found " << *search << '\n';
    else
        std::cout << "Not found\n";
 
    std::unordered_set<std::string, string_hash, std::equal_to<>> set{"one"s, "two"s};
 
    logger::enabled = true;
    std::cout << "Heterogeneous lookup for unordered containers (transparent hashing):\n"
              << (set.find("one")   != set.end()) << '\n'
              << (set.find("one"s)  != set.end()) << '\n'
              << (set.find("one"sv) != set.end()) << '\n';
}

# See also