37

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());
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Loom
  • 9,768
  • 22
  • 60
  • 112
  • 1
    Have you read any documentation? This is really easy to find out via googling. – Björn Pollex May 06 '11 at 09:50
  • well, they are two different iterators, does that help? aside from that, you'll be splitting hairs... (i.e. profile to find out!) – Nim May 06 '11 at 10:03
  • 2
    @Nim - I wonder how it is written in standard – Loom May 06 '11 at 12:49
  • 1
    if you mean the C++ standard, you'll have to ask one of the standards lawyers on this site.. I haven't a clue... As for usage, frankly I think it's daft that this *hint* has to be provided in the first place, but if I had to use it, I'd always use `end()` – Nim May 06 '11 at 12:55

2 Answers2

33

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

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
James Kanze
  • 150,581
  • 18
  • 184
  • 329
7

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.

zrb
  • 721
  • 1
  • 6
  • 15