Efficiently initialise std::set with a sequence of numbers

后端 未结 5 1819
野的像风
野的像风 2020-12-31 01:29

An obvious (naive?) approach would be:

std::set s;
for (int i = 0; i < SIZE; ++i) {
    s.insert(i);
}

That\'s reasonable rea

5条回答
  •  温柔的废话
    2020-12-31 02:11

    Well you can use the insert() version of set<> in which you can provide the position as hint where the element might get inserted.

    iterator insert ( iterator position, const value_type& x );
    

    Complexity: This version is logarithmic in general, but amortized constant if x is inserted right after the element pointed by position.

提交回复
热议问题