In C++23, std::set::insert_range()adds every value of a range to the 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 set stays sorted. The function needs a C++23 compiler and <set>.
This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Inserts every value of range. Values already in the 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 set stores one, so the letters are e, h, l, o in alphabetical order.
The string has 5 characters while the 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 set is the same.
No. Values that are already in the set, or repeated in the range, are skipped. The 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 set value type, such as a vector, an array, a string, or another set.