Probably not one more efficient, no.
std::generate
(or in this case, more directly, std::iota
) can also do what you want, but off the top of my head I can't see a particular benefit over a simple for
loop, at least in terms of performance.
Ultimately, whatever you end up doing, will need to create lots of tree nodes with these values, and at some level the computer needs to iterate to do this. You don't get any benefit from pre-allocating here because that notion is meaningless with a set (though this would be a valid suggestion for a vector).
(Edit: I can't take credit for this, but rodrigo points out that an insert hint may help. Anecdotally I heard somewhere that hints tend to be ignored nowadays but I'm not sure how true that is.)
The best way is to try not to want this. Usually if you have a collection of N consecutive integers, you can find a way to not need it, be it through some magic maths or at least through generating only what you need on demand. I suppose there is an exception to every rule though.