0
votes

Hi I am using the forward list STL library in VS 2019. This is my block of code:

forward_list<int> fl1;
    fl1.assign({ 1,2,3,4 });
    fl1.push_front(0);
    fl1.pop_front();
    cout << "Front " << fl1.front() << endl;
    forward_list<int>::iterator it4 = fl1.begin();
    it4 = fl1.insert_after(it4, 5);
    fl1.emplace_front(6);
    fl1.remove(6);
    forward_list<int> fl2;
    fl2.assign({ 9,8,7,6,6 });
    fl2.unique();
    fl2.sort();
    fl1.sort();
    fl2.reverse();
    fl1.merge(fl2);
   // fl1.clear();

    for (int i : fl1)
        cout << i << endl;

Once the program hits the merge line, it gives me a "Debug Assertion Failed Line: 1492 Expression: Sequence not ordered" my code is only 500 lines long and the code itself occurs at line 342 where I have that for loop that is supposed to handle iterate through fl1: for (int i : fl1) cout << i << endl;

When I hit abort and see the debug error, it says "Unhandled exception at 0x5049F2F6 (ucrtbased.dll) in Derektut.exe: An invalid parameter was passed to a function that considers invalid parameters fatal"

Why is this?

1
To use merge(), the lists need to be sorted, and your fl1 list is not sorted. See the documentation - PaulMcKenzie
So i added that sort command and it still gave me the same error @PaulMcKenzie - edo101
You should post a minimal reproducible example. First, the sort function sorts in ascending order, and your other list has descending sort order. How is that going to work? - PaulMcKenzie
@PaulMcKenzie I actually had that sort command initially but took it off from my post because when I checked it wasn't the solution so I just took it off. That was the very first thing I checked - edo101
If the lists are not sorted, the behavior is undefined, meaning anything can happen, including "work". So your tutor's code is wrong, but they didn't know it since the compiler's runtime didn't check, and is not required to check. The Visual Studio debug runtime library will actually check if the lists are sorted, and will assert if the lists are not sorted. Now run your code in "Release" mode under Visual Studio -- do you see an error? If not, then that's the issue your tutor was dealing with -- bad code, but no indication the code was bad. - PaulMcKenzie

1 Answers

0
votes

The std::forward_list::merge requires that both lists are sorted. Your fl1 list is not sorted when fl1.merge() is called. Calling merge on an unsorted list is undefined behavior.

As to the Debug Assertion error:

Since this is a Visual Studio generated error, in Visual Studio, the debug runtime will check if both lists are sorted when making the call to merge, and will assert() if one of the lists is not sorted.