2

According to cplusplus.com, the std::set::insert() overload that takes a hint iterator of where to insert an item changes from C++98 to C++11. In C++98 the hint should be:

The function optimizes its insertion time if position points to the element that will precede the inserted element.

However, in C++11 the hint is changed and should now be:

The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).

But, in both C++98 or C++11, the return value is the same:

An iterator pointing to either the newly inserted element or to the element that already had its same value in the set.

For C++98, I have code to insert a sequence of adjacent items as follows:

void example98(std::set &_sent, int beginOffset, int lastOffset) {
  std::set<int>::iterator itr = _sent.end();

  for (int offset = beginOffset; offset <= lastOffset; ++offset) {
    itr = _sent.insert(itr, offset);
  }
}

I could change this in C++11 to be:

void example11(std::set &_sent, int beginOffset, int lastOffset) {
  std::set<int>::iterator itr = _sent.end();

  for (int offset = lastOffset; offset >= beginOffset; --offset) {
    itr = _sent.insert(itr, offset);
  }
}

But am bothered by the need to refactor the code to go from C++98 to C++11, am I doing something wrong here, or if not, what was the motivation for this change, and why was the argument to insert() changed but not the return value?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • Assuming your set is sorted in ascending order, your `example98` actually matches the C++11 wording. You start off with the `end()` iterator as the hint, this will never point to the element that will precede the newly inserted element. A correct hint according to the C++98 hint would be `std::prev(_sent.end())` assuming the `set` is empty, which brings us to another defect of that wording - you can never indicate that the new element should be inserted at the head of the `set`. I'm closing this as a dupe of the question linked by quantdev. You don't need to refactor your code. – Praetorian Jan 04 '15 at 00:18
  • @Praetorian The set is default sorted in ascending order. Given that the C++98 standard is in error, I believe insert wants a hint to an element that will follow the item to be inserted. That being the case, I think my `example11()` code is the correct one to use, right? – WilliamKF Jan 04 '15 at 00:34
  • Yeah, you're I got it wrong earlier. Your `example11` should work, or you could modify `example98` and fix the hint to `_sent.end()` assuming the range of new elements being inserted are greater than existing elements in the set. – Praetorian Jan 04 '15 at 01:57
  • @Praetorian I believe the initial value for itr is not important as it is only one iteration of the loop that the value is wrong and a find would be required to initialize it correctly since new values being inserted could be anywhere relative to the existing values, adding complexity to the code with no significant performance benefit. – WilliamKF Jan 04 '15 at 03:16

0 Answers0