对std::inserter对std::set使用.begin()和.end()有区别吗?

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

本文关键字:std end 有区别 begin inserter set 使用      更新时间:2023-10-16

如果it1和it2有什么不同?

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

实际上并不多。如果你要在一个空的set中插入大量已经有序的元素,第二种方法会快一些,但仅此而已。std::insert_iterator用迭代器调用insert;std::set将其解释为提示,如果插入在提示之前,则以常数时间(而不是lgn)插入。(实际上,如果set是空的,我认为两者将做完全相同的事情。)

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

对于排序关联容器,insert_iterator的构造函数中的迭代器几乎是无关的。新元素不一定会形成一个连续的范围;它们将以键升序出现在容器中的适当位置。它们插入的顺序只影响效率:将已经排序的范围插入排序关联容器是一个O(N)操作。