Contents Up Previous Next

wxList<T>

The wxList<T> class provides linked list functionality. It has been written to be type safe and to provide the full API of the STL std::list container and should be used like it. The exception is that wxList<T> actually stores pointers and therefore its iterators return pointers and not references to the actual objets in the list (see example below). In other words value_type is defined as T*.

Unfortunately, the new wxList<T> class requires that you declare and define each wxList<T> class in your program. This is done with WX_DECLARE_LIST and WX_DEFINE_LIST macros (see example). We hope that we'll be able to provide a proper template class providing both the STL std::list and the old wxList API in the future.

Please refer to the STL std::list documentation for further information on how to use the class. Below we documented both the supported STL and the legacy API that originated from the old wxList class and which can still be used alternatively for the the same class.

Note that if you compile wxWidgets in STL mode (wxUSE_STL defined as 1) then wxList<T> will actually derive from std::list and just add a legacy compatibility layer for the old wxList class.

Example

    // this part might be in a header or source (.cpp) file
    class MyListElement
    {
        ... // whatever
    };

    // this macro declares and partly implements MyList class
    WX_DECLARE_LIST(MyListElement, MyList);

    ...

    // the only requirement for the rest is to be AFTER the full declaration of
    // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but
    // usually it will be found in the source file and not in the header

    #include <wx/listimpl.cpp>
    WX_DEFINE_LIST(MyList);


    MyList list;
    MyListElement element;
    list.Append(&element);     // ok
    list.Append(17);           // error: incorrect type

    // let's iterate over the list in STL syntax
    MyList::iterator iter;
    for (iter = list.begin(); iter != list.end(); ++iter)
    {
        MyListElement *current = *iter;

        ...process the current element...
    }

    // the same with the legacy API from the old wxList class
    MyList::compatibility_iterator node = list.GetFirst();
    while (node)
    {
        MyListElement *current = node->GetData();

        ...process the current element...
        
        node = node->GetNext();
    }

For compatibility with previous versions wxList and wxStringList classes are still defined, but their usage is deprecated and they will disappear in the future versions completely. The use of the latter is especially discouraged as it is not only unsafe but is also much less efficient than wxArrayString class.

Include files

<wx/list.h>

Library

wxBase

See also

wxArray

Members

wxList<T>::wxList<T>
wxList<T>::~wxList<T>
wxList<T>::Append
wxList<T>::Clear
wxList<T>::DeleteContents
wxList<T>::DeleteNode
wxList<T>::DeleteObject
wxList<T>::Erase
wxList<T>::Find
wxList<T>::GetCount
wxList<T>::GetFirst
wxList<T>::GetLast
wxList<T>::IndexOf
wxList<T>::Insert
wxList<T>::IsEmpty
wxList<T>::Item
wxList<T>::Member
wxList<T>::Nth
wxList<T>::Number
wxList<T>::Sort
wxList<T>::assign
wxList<T>::back
wxList<T>::begin
wxList<T>::clear
wxList<T>::empty
wxList<T>::end
wxList<T>::erase
wxList<T>::front
wxList<T>::insert
wxList<T>::max_size
wxList<T>::pop_back
wxList<T>::pop_front
wxList<T>::push_back
wxList<T>::push_front
wxList<T>::rbegin
wxList<T>::remove
wxList<T>::rend
wxList<T>::resize
wxList<T>::reverse
wxList<T>::size
wxList<T>::splice


wxList<T>::wxList<T>

wxList<T>()

wxList<T>(size_t count, T *elements[])

Constructors.


wxList<T>::~wxList<T>

~wxList<T>()

Destroys the list, but does not delete the objects stored in the list unless you called DeleteContents(true ).


wxList<T>::Append

wxList<T>::compatibility_iterator Append(T *object)

Appends the pointer to object to the list.


wxList<T>::Clear

void Clear()

Clears the list, but does not delete the objects stored in the list unless you called DeleteContents(true ).


wxList<T>::DeleteContents

void DeleteContents(bool destroy)

If destroy is true, instructs the list to call delete on objects stored in the list whenever they are removed. The default is false.


wxList<T>::DeleteNode

bool DeleteNode(const compatibility_iterator&iter)

Deletes the given element refered to by iter from the list, returning true if successful.


wxList<T>::DeleteObject

bool DeleteObject(T *object)

Finds the given object and removes it from the list, returning true if successful. The application must delete the actual object separately.


wxList<T>::Erase

void Erase(const compatibility_iterator&iter)

Removes element refered to be iter.


wxList<T>::Find

wxList<T>::compatibility_iterator Find(T * object) const

Returns the iterator refering to object or NULL if none found.


wxList<T>::GetCount

size_t GetCount() const

Returns the number of elements in the list.


wxList<T>::GetFirst

wxList<T>::compatibility_iterator GetFirst() const

Returns the first iterator in the list (NULL if the list is empty).


wxList<T>::GetLast

wxList<T>::compatibility_iterator GetLast() const

Returns the last iterator in the list (NULL if the list is empty).


wxList<T>::IndexOf

int IndexOf(T* obj ) const

Returns the index of obj within the list or wxNOT_FOUND if obj is not found in the list.


wxList<T>::Insert

wxList<T>::compatibility_iterator Insert(T *object)

Insert object at the front of list.

wxList<T>::compatibility_iterator Insert(size_t position, T *object)

Insert object before position, i.e. the index of the new item in the list will be equal to position. position should be less than or equal to GetCount; if it is equal to it, this is the same as calling Append.

wxList<T>::compatibility_iterator Insert(compatibility_iteratoriter, T *object)

Inserts the object before the object refered to be iter.


wxList<T>::IsEmpty

bool IsEmpty() const

Returns true if the list is empty, false otherwise.


wxList<T>::Item

wxList<T>::compatibility_iterator Item(size_t index) const

Returns the iterator refering to the object at the given index in the list.


wxList<T>::Member

wxList<T>::compatibility_iterator Member(T * object) const

NB: This function is deprecated, use Find instead.


wxList<T>::Nth

wxList<T>::compatibility_iterator Nth(int n) const

NB: This function is deprecated, use Item instead.

Returns the nth node in the list, indexing from zero (NULL if the list is empty or the nth node could not be found).


wxList<T>::Number

int Number() const

NB: This function is deprecated, use GetCount instead.

Returns the number of elements in the list.


wxList<T>::Sort

void Sort(wxSortCompareFunction compfunc)

  // Type of compare function for list sort operation (as in 'qsort')
  typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2);
Allows the sorting of arbitrary lists by giving a function to compare two list elements. We use the system qsort function for the actual sorting process.


wxList<T>::assign

void assign(const_iterator first, const const_iterator& last)

void assign(size_type n, const_reference v = value_type())


wxList<T>::back

reference back()

const_reference back() const

Returns the last item of the list.


wxList<T>::begin

iterator begin()

const_iterator begin() const

Returns a (const) iterator pointing to the beginning of the list.


wxList<T>::clear

void clear()

Removes all items from the list.


wxList<T>::empty

bool empty() const

Returns true if the list is empty.


wxList<T>::end

iterator end()

const_iterator end() const

Returns a (const) iterator pointing at the end of the list.


wxList<T>::erase

iterator erase(const iterator& it)

Erases the item pointed to by it.

iterator erase(const iterator& first, const iterator& last)

Erases the items from first to last.


wxList<T>::front

reference front()

const_reference front() const

Returns the first item in the list.


wxList<T>::insert

iterator insert(const iterator& it, const_reference v = value_type())

void insert(const iterator& it, size_type n, const_reference v = value_type())

void insert(const iterator& it, const_iterator first, const const_iterator& last)

Inserts an item (or several) at the given position.


wxList<T>::max_size

size_type max_size() const

Returns the largest possible size of the list.


wxList<T>::pop_back

void pop_back()

Removes the list item.


wxList<T>::pop_front

void pop_front()

Removes the first item.


wxList<T>::push_back

void push_back(const_reference v = value_type())

Adds an item to end of the list.


wxList<T>::push_front

void push_front(const_reference v = value_type())

Adds an item to the front of the list.


wxList<T>::rbegin

reverse_iterator rbegin()

const_reverse_iterator rbegin() const

Returns a (const) reverse iterator pointer to the beginning of the reversed list.


wxList<T>::remove

void remove(const_reference v)

Removes an item from the list.


wxList<T>::rend

reverse_iterator rend()

const_reverse_iterator rend() const

Returns a (const) reverse iterator pointer to the end of the reversed list.


wxList<T>::resize

void resize(size_type n, value_type v = value_type())

Resizes the list. If the the list is enlarges items with the value v are appended to the list.


wxList<T>::reverse

void reverse()

Reverses the list.


wxList<T>::size

size_type size() const

Returns the size of the list.


wxList<T>::splice

void splice(const iterator& it, wxList<T>& l)

void splice(const iterator& it, wxList<T>& l, const iterator& first)

void splice(const iterator& it, wxList<T>& l, const iterator& first, const iterator& last)

Moves part of the list into another list, starting from first and ending at last if specified.