Is there a difference between using .begin() vs .end() for std::inserter for std::set?

前端 未结 2 1387
旧时难觅i
旧时难觅i 2021-01-01 10:59

If there is any difference between it1 and it2?

std::set s;

auto it1 = std::inserter(s, s.begin());
auto it2 = std::inserter(s, s.end());
<         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 11:04

    In practice, not much. If you're inserting a large number of already in order elements into an empty set, the second will be somewhat faster, but that's about it. std::insert_iterator calls insert with the iterator; std::set interprets it as a hint, and inserts in constant time (rather than lg n) if the insertion is immediately before the hint. (Actually, if the set is empty, I think both will do exactly the same thing.)

提交回复
热议问题