The standard specifies (23.4.4.2:5, etc.) that constructing all four ordered associative containers (map
, multimap
, set
, multiset
) from a range [first, last)
shall be linear in N = last - first
if the range is already sorted.
For merging a range (e.g. a container of the same type) into an existing container, however, 23.2.4:8 table 102 specifies only that insert
ing a range [i, j)
into the container shall have complexity N log(a.size() + N)
where N = distance(i, j)
. This would seem to indicate that using the hinted insert:
for (auto it = a.begin(); i != j; ++i)
it = next(a.insert(it, *i));
could be more efficient than a.insert(i, j)
, having complexity strictly less than N log(a.size() + N)
, depending on the proportion of hints that are correct (except in the trivial case where N == 0
); and being linear, since every hint is correct, if a
is initially empty.
Is this a deficiency in the standard, or is there some other language (or facility) that covers this case? In practice, do implementations optimise for merging an ordered associative container into another?
Credit to https://stackoverflow.com/a/11362162/567292 for inspiring this question.