The efficient way of inserting already ordered elements into a set is to hint the library as to where the next element will be. For that you want to use the version of insert
that takes an iterator:
std::set<int>::iterator it = mySet.end();
for (int x : input) {
it = mySet.insert(it, x);
}
On the other hand, you might want to consider other containers. Whenever possible, use std::vector
. If the amount of insertions is small compared to lookups, or if all inserts happen upfront, then you can build a vector, sort it and use lower_bound
for lookups. In this case, since the input is already sorted, you can skip the sorting.
If insertions (or removals) happen all over the place, you might want to consider using std::unordered_set<int>
which has an average O(1)
insertion (per element) and lookup cost.
For the particular case of tracking small numbers in a set, all of which are small (34 to 75 are small numbers) you can also consider using bitsets or even a plain array of bool
in which you set the elements to true
when inserted. Either will have O(n)
insertion (all elements) and O(1)
lookup (each lookup), which is better than the set.