0
votes

Hello everybody can you help me? the problem is that when a person enters a element from list he can insert before this element elements from another list, but I constantly knock out mistakes. Maybe someone will help? It looks like: 3 6 7 9 user enters 6 he can create another list like 8 7 5 and output is 3 8 7 5 6 7 9 The error

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int size
int size1;
int el1;
int znach=0;
    cout << "Enter size: " << endl;
        cin >> size;
         list<int>lis;
            list<int>lis1;
            list <int> ::iterator it;
            int s = lis.size() / 2;
            auto it1 = lis.begin();
            advance(it1, s);
            for (int i = 0; i <size; i++)
            {
                cout << "Enter " << i << " element: ";
                cin >> t;
                lis.push_back(t);
            }
            cout << "Enter element: " << endl;
            cin >> el1;
            for (it = lis.begin(); it != lis.end(); it++)
            {

                if (*it = el1)
                {
                    znach++;
                }
            }
                 if (znach == 0)
                    {
                        cout << "There is no element;" << endl;
                    }
        else
                {
                    cout << "Enter size of new vector: " << endl;
                    cin >> size1;
                    for (int i = 0; i < size1; i++)
                    {
                        int t1;
                        cout << "Enter " << i << " element: ";
                        cin >> t1;
                        lis1.push_back(t1);
                    }
                    auto it2 = lis.begin();
                    while (*it2 != el1)
                    {
                        it2++;
                    }
                    --it2;
                    lis.splice(it2, lis1);
                }


            for (it = lis.begin(); it != lis.end(); it++)
            {
                cout << *it<<" ";
            }
        }
1
what debug assertion failed? - 463035818_is_not_a_number
what is size ? Please provide a minimal reproducible example - 463035818_is_not_a_number
I'm sorry, updated. - Sevka
you should include the message as text in the question (not everybody can view images). It says "cannot decrement begin iterator" and there is one place where you decrement an iterator, now try to guess where the problem is ;) - 463035818_is_not_a_number
@idclev463035818 If I delete line --it2; It works well, but output is wrong. I enters first list 1 5 7, then I enters that i want to put second list before num (second list is 9 8) and output is 9 8 5 5 5 - Sevka

1 Answers

0
votes

I don't know what your code is doing, and maybe there are other problems. However, the runtime error you get is caused by

auto it2 = lis.begin();
while (*it2 != el1)
{
    it2++;
}
--it2;

When *lis.begin() == el1 then you never increment it2 and then decrement it, but you cannot decrement the begin iterator. Usually thats just undefined, but as you compiled a debug build you got an assertion fired up that tells you what went wrong.