Access the list item specified by the iterator

The natural answer would be to dereference the iterator and get the value. However, I am stuck with using VC ++ 2010, which does not allow to dereference a list iterator (or does it?) I am confused because at some point I need to dereference two list iterators and compare their values ​​using: (* it) == (* it2) The program crashes with an error, only because of this line. I also dereference the iterator in the statement: printf ("% d \ n", (* it)); It works great. So, is there a way to access an element without dereferencing or using cliext :: list.

for (it=sList.begin(); it != sList.end(); it++)
{
    for (it2=it; it2 != sList.end(); it2++)
    {
        it2++;
        if ((*it) == (*it2))
        {
           sList.erase(it, it2);
        }
        it2--;
    }
}

The error I get is:

Debugging error

Expression: enumerate an iterator not reversible

It is surprising that the same code works without problems when compiling in DevC ++ (MinGW)

+3
5

list. , . , end, , , . .

EDIT: , . , , . , , /.

EDIT2: , . .

- :

for (it=sList.begin(); it != sList.end(); ++it)
{
    it2 = it;
    ++it2;
    while(it2 != sList.end())
    {
        if ((*it) == (*it2))
        {
           it = it2 = sList.erase(it, it2);  // Reset it as well since it will be blown away. It'll still point to the same value it did before though.
        }
        else
            ++it2;
    }
}
+3

"" Isnt Broken.

. , . -

- , MS. , , . , . : ++? !:)

UPDATE:

, , . . , .

std::list<int>::iterator EraseElements(std::list<int>& sList, std::list<int>::iterator start)
{
    for (std::list<int>::iterator itor1 = start; itor1 != sList.end(); ++itor1)
    {
        std::list<int>::iterator itor2(itor1);
        ++itor2;

        for ( ; itor2 != sList.end(); ++itor2)
        {
            if ((*itor1) == (*itor2))
            {
                return sList.erase(itor1, itor2);
            }
        }
    }

    return sList.end();
}


void main()
{
    // Test
    list<int> sList;
    sList.push_back(1);

    // elements will be erased
    sList.push_back(2);
    sList.push_back(3);
    //
    sList.push_back(2);

    sList.push_back(4);
    sList.push_back(5);

    // elements will be erased
    sList.push_back(6);
    sList.push_back(7);
    //
    sList.push_back(6);


    list<int>::iterator next = sList.begin();
    while (next != sList.end())
    {
        next = EraseElements(sList, next);
    }

    // It will print 1 2 4 5 6
    for (std::list<int>::const_iterator itor = sList.begin(); itor != sList.end(); ++itor)
    {
        cout << *itor << endl;
    }
}
+1

, , . . .

for (it2=it; it2 != sList.end(); it2++)
    {
        it2++;
            // there is no guarantee that it2 will now be valid
            // it should be validated again
        if ((*it) == (*it2))
        {
            // you should not modify the list here.
            // this will invalidate your iterators by default.
           sList.erase(it, it2);
        }
        it2--;
    }
+1

:

for (it=sList.begin(); it != sList.end(); it++)
{
    for (it2=sList.end()-1; it2 != it+1; it2--)
    {
        if ((*it) == (*it2))
        {
            it = sList.erase(it, it2)-1;
            break;
        }
    }
}

. -, . for it2 sList.end()-1, sList.end() . () , ( , end, ).

-, erase , ( it it2-1). , , , break , . erase ( , it). for it, 1 , erase, it . ( , , it , , , , ).

, 0 2 3 4 5 1 6 7 8 0 9 10 11 1. , ( 0 1 's, , 0 1?), 0 , 9 10 11 1 's.

+1

, - , .

, , , , , .

, .

, / , .

it2, , ( ).

0

Source: https://habr.com/ru/post/1795300/


All Articles