0
void one_number_deletion(std::list<int> &tabela)
{

    std::cout << "What value would u like to delete: " << std::endl;
    int value, spr=0;
    std::cin >> value;
    std::list<int>::iterator itr = tabela.begin();
    for (itr; itr!=tabela.end();)
    {



        if (*itr == value)          //do not increment itr in the loop there
        {
            spr = 1;
            //tabela.erase(itr);        //the problem

        }
        else ++itr;

    }
    if (spr == 0) std::cout << "Wrong table value  " << std::endl;

}

The idea is that I want an user to type 10 values to the vector/list and then I want him to choose 1 value which he would like to delete. The code shows only the function, which is getting the list/vector from the main funct. Everything works correctly untill I switch tabela.erase(itr) on, then I get the error that iterator is incompatible. I have no idea how can I make this differently.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • `std::list` provides `remove_if` method, for `std::vector` you should use erase-remove idiom instead of writing a loop – Slava Feb 04 '20 at 21:32
  • 1
    @Slava or wait until `C++20` for [`std::erase_if`](https://en.cppreference.com/w/cpp/container/vector/erase2). – Fureeish Feb 04 '20 at 21:33
  • @Fureeish we waited for concepts and modules for 20 years, we can wait for `std::erase_if` even more – Slava Feb 04 '20 at 21:36

0 Answers0