In C++23, std::unordered_set::insert_range()adds every value of a range to the unordered_set. A range can be a std::vector, a std::array, a string, or a view. You pass the whole object, not two iterators.
Each value is inserted as if by insert(), so repeats are skipped and the unordered_set stays sorted. The function needs a C++23 compiler and <unordered_set>.
This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
Inserts every value of range. Values already in the unordered_set are skipped. Returns nothing.
Before C++23 you wrote the same thing as s.insert(v.begin(), v.end()). Both do the same work. insert_range() is shorter and works with range views too. If your compiler does not support C++23 yet, use the two-iterator insert().
A std::string is a range of char values, so insert_range(word) works with no extra code.
The word hello has two letter l characters. The unordered_set stores one, so the letters are e, h, l, o in an unspecified order.
The string has 5 characters while the unordered_set has 4 different ones. The original string is not changed.
Frequently Asked Questions
It was added in C++23. Compile with a flag such as -std=c++23 and use a library that supports it.
insert(first, last) takes two iterators. insert_range(range) takes the whole range object, and it also works with range views. The result in the unordered_set is the same.
No. Values that are already in the unordered_set, or repeated in the range, are skipped. The unordered_set keeps one copy of each.
No, it returns void. Compare size() before and after the call if you need the count.
Any range whose elements convert to the unordered_set value type, such as a vector, an array, a string, or another set.