0

I'm currently reading a book on C++11: "Exploring C++ 11 by Ray Lischner". I like it because I found it really useful and simple in explaining. The problem. Here "Listing 13-2. Local Variable Definitions" Where I get the issue:

int main() {

    std::vector<int> data{ 10, 57, 23, 81, 7, 5, 24 }; // I removed the line below in order not everytime I enter code.
//  data.insert(data.begin(), std::istream_iterator<int>(std::cin),
    //  std::istream_iterator<int>());
    for (std::vector<int>::iterator iter{ data.begin() }, end{ data.end() }; iter != end; ) {
        int value{ *iter };
        std::vector<int>::iterator here{ std::lower_bound(data.begin(), iter, value) };
        if (iter == here)
            ++iter; // already in sorted position
        else {
            // erase the out-of-position item, advancing iter at the same time.
            iter = data.erase(iter);
            data.insert(here, value);
        }
    }

    for (std::vector<int>::iterator iter{ data.begin() }, prev{ data.end() }, end{ data.end() };
    iter != end; ++iter) {
        if (prev != data.end())
            assert(not (*iter < *prev));
        prev = iter;
    }
    std::cout << '{';
    std::string separator{ " " };

    for (int element : data)
    {
        std::cout << separator << element;
        separator = ", ";
    }
    std::cout << " }\n";


    std::cout << std::endl;
    std::system("pause");
}
  • So as you can see above the code is to sort a vector of integers. But When I run the program on MSVC14.0 I get the assertion dialog: "Vector iterator not dereferenceable". int value{*iter}.

  • What is the point in: iter = data.erase(iter);? I mean the return value (What is the point in the return value here? I tried writting: data.erase(iter); and works on GCC like charm.).

I tried the very code on GCC on ideone and it works fine!?

Please help me. Thank you in advance.

HolyBlackCat
  • 45,832
  • 5
  • 81
  • 134
Alex24
  • 590
  • 2
  • 12
  • 1
    You make a copy of `data.end()`. When you `.erase()` from (or `.insert()` to) `data`, the copy gets invalidated. The loop condition should be changed to `iter != data.end()`. – HolyBlackCat Jan 09 '19 at 20:45
  • @HolyBlackCat: I changed the loop condition but the problem is still the same. – Alex24 Jan 09 '19 at 20:49
  • *"What is the point in: iter = data.erase(iter);?"* Formally, `.erase()` invalidates the iterator passed to it, so it can't be used anymore. You have to rely on its' return value to get an iterator to the next valid element. Practically, `.erase()` almost always returns the parameter unchanged. Unless your compiler specifically checks this case, this UB tends to have no effect. – HolyBlackCat Jan 09 '19 at 20:49
  • Which line triggers the assetion? – HolyBlackCat Jan 09 '19 at 20:54
  • @HolyBlackCat: `int value{ *iter };` – Alex24 Jan 09 '19 at 21:00
  • What sorting algorithm erases items? Maybe swap them, but erase them? – PaulMcKenzie Jan 09 '19 at 21:02
  • Apparently that's because `.insert` invalidates `iter`. Instead of erasing/inserting, I suggest using `std::rotate` instead. It should be more simple and more efficient. – HolyBlackCat Jan 09 '19 at 21:06
  • @PaulMcKenzie: The sorting algorithm is as you can see above: `std::lower_bound()` which returns an iterator to the first value greater than or equal to a passed value in the range: `begin()... iterator`. – Alex24 Jan 09 '19 at 21:07
  • @Alex24 `lower_bound` works on sorted ranges. It uses a binary search, not a linear search. So giving `lower_bound` an unsorted vector is gumming up the works. – PaulMcKenzie Jan 09 '19 at 21:11
  • @PaulMcKenzie: Would I give up reading this book? What would you recommend to read on `C++11`? Also Why on GCC the code works fine??? I am really stuck?!. – Alex24 Jan 09 '19 at 21:15
  • 1
    Never read the book, so won't comment. But if you want a page on sorting and C++, then [here is a link](https://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c). – PaulMcKenzie Jan 09 '19 at 21:17
  • 1
    Here is a curated list of good C++ books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Eljay Jan 09 '19 at 21:17

0 Answers0