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

半腔热情 提交于 2019-12-29 06:34:07

问题


If there is any difference between it1 and it2?

std::set<sometype> s;

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

回答1:


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.)




回答2:


From http://www.sgi.com/tech/stl/insert_iterator.html

In the case of a Sorted Associative Container, however, the iterator in the insert_iterator's constructor is almost irrelevant. The new elements will not necessarily form a contiguous range; they will appear in the appropriate location in the container, in ascending order by key. The order in which they are inserted only affects efficiency: inserting an already-sorted range into a Sorted Associative Container is an O(N) operation.



来源:https://stackoverflow.com/questions/5909624/is-there-a-difference-between-using-begin-vs-end-for-stdinserter-for-std

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!