对于 std::set 的 std::inserter 使用 .begin() 与 .end() 之间有区别吗?
it1和it2有什么区别吗?
std::set<sometype> s;
auto it1 = std::inserter(s, s.begin());
auto it2 = std::inserter(s, s.end());
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,并不多。如果您将大量已排序的元素插入到空的
set
中,则第二个会稍快一些,但仅此而已。std::insert_iterator
使用迭代器调用insert
;std::set
将其解释为提示,如果插入紧接在提示之前,则以恒定时间(而不是 lg n)插入。 (实际上,如果set
为空,我认为两者都会做完全相同的事情。)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
callsinsert
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 theset
is empty, I think both will do exactly the same thing.)来自 http://www.sgi.com/tech/stl/insert_iterator.html
然而,在有序关联容器的情况下,insert_iterator 构造函数中的迭代器几乎是无关紧要的。新元素不一定会形成连续的范围;它们将按键升序出现在容器中的适当位置。它们的插入顺序仅影响效率:将已排序的范围插入排序关联容器是一个 O(N) 操作。
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.