简体   繁体   中英

How to check unordered_set for overlaping?

I have two unordered_sets and need to check if all the elements of the first one are also elements of the second one.

Is there a fast way to do this or should I use another container?

Just use a loop (or a corresponding algorithm). The complexity is (approximately) linear in the size of the range to be tested.

template <typename UnorderedSet, typename Iterator>
bool contains_all(UnorderedSet&& set, Iterator first, Iterator last)
{
    using value_type = std::iterator_traits<Iterator>>::value_type;
    return std::all_of(first, last, [&set] (const value_type& value) {
        return set.count(value);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM