문제

std::list is a double linked list. Doesn't that mean that it should be possible to remove an item from a list by only having access to the iterator?

Maybe my question wasn't clear enough.

#pragma once

#include <list>


typedef std::list<int> IntList ;
typedef IntList::iterator IntIterator;

class IntHiddenList
{
private:
    IntList list;
public:
    IntIterator AddInt(int x)
    {
        list.push_front(x);
        return list.begin();
    }
};

int main()
{
    IntHiddenList a;

    IntIterator it = a.AddInt(5);


    // How would I go about deleting 5 from the list using only "it"?
}
도움이 되었습니까?

해결책

Yes, notionally it's possible. However, the standard library does not allow it (it requires the container and iterator to erase).

However you're in luck: boost provides the boost::instrusive (http://www.boost.org/doc/libs/1_54_0/doc/html/intrusive/list.html) capability to do exactly what you want.

다른 팁

No, you will still need the list in order to delete an element.

In STL, iterator only holds the pointer to the data, and provides operations to move through the container. You can see good table description here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top