CopperSpice API  1.7.2
QLinkedList< T > Class Template Reference

Template class which stores values in a linked list. More...

Classes

class  const_iterator
 The QLinkedList::const_iterator class provides an STL const iterator for QLinkedList More...
 
class  iterator
 The QLinkedList::iterator class provides an STL style iterator for QLinkedList More...
 

Public Typedefs

using allocator_type = typename std::list< T >::allocator_type
 
using const_iterator = typename std::list< T >::const_iterator
 
using const_pointer = typename std::list< T >::const_pointer
 
using const_reference = typename std::list< T >::const_reference
 
using const_reverse_iterator = typename std::list< T >::const_reverse_iterator
 
using difference_type = typename std::list< T >::difference_type
 
using iterator = typename std::list< T >::iterator
 
using Java_Iterator = QLinkedListIterator< T >
 
using Java_MutableIterator = QMutableLinkedListIterator< T >
 
using pointer = typename std::list< T >::pointer
 
using reference = typename std::list< T >::reference
 
using reverse_iterator = typename std::list< T >::reverse_iterator
 
using size_type = typename std::list< T >::difference_type
 
using value_type = typename std::list< T >::value_type
 

Public Methods

 QLinkedList () = default
 
 QLinkedList (const QLinkedList< T > &other) = default
 
template<class Input_Iterator >
 QLinkedList (Input_Iterator first, Input_Iterator last)
 
 QLinkedList (QLinkedList< T > &&other) = default
 
 QLinkedList (std::initializer_list< T > args)
 
 ~QLinkedList () = default
 
void append (const QLinkedList< T > &other)
 
void append (const T &value)
 
void append (T &&value)
 
reference back ()
 
const_reference back () const
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
bool contains (const T &value) const
 
size_type count () const
 
size_type count (const T &value) const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
bool endsWith (const T &value) const
 
iterator erase (const_iterator begin, const_iterator end)
 
iterator erase (const_iterator pos)
 
reference first ()
 
const_reference first () const
 
reference front ()
 
const_reference front () const
 
iterator insert (iterator before, const T &value)
 
iterator insert (iterator before, size_type count, const T &value)
 
bool isEmpty () const
 
reference last ()
 
const_reference last () const
 
size_type length () const
 
bool operator!= (const QLinkedList< T > &other) const
 
QLinkedList< T > operator+ (const QLinkedList< T > &other) const
 
QLinkedList< T > & operator+= (const QLinkedList< T > &other)
 
QLinkedList< T > & operator+= (const T &value)
 
QLinkedList< T > & operator<< (const QLinkedList< T > &other)
 
QLinkedList< T > & operator<< (const T &value)
 
QLinkedList< T > & operator= (const QLinkedList< T > &other) = default
 
QLinkedList< T > & operator= (QLinkedList< T > &&other) = default
 
bool operator== (const QLinkedList< T > &other) const
 
void pop_back ()
 
void pop_front ()
 
void prepend (const T &value)
 
void push_back (const T &value)
 
void push_front (const T &value)
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
size_type removeAll (const T &value)
 
void removeFirst ()
 
void removeLast ()
 
bool removeOne (const T &value)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
size_type size () const
 
bool startsWith (const T &value) const
 
void swap (QLinkedList< T > &other)
 
takeFirst ()
 
takeLast ()
 
std::list< T > toStdList () const
 

Static Public Methods

static QLinkedList< T > fromStdList (const std::list< T > &other)
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &out, const QLinkedList< T > &list)
 
QDataStreamoperator>> (QDataStream &in, QLinkedList< T > &list)
 

Detailed Description

template<class T>
class QLinkedList< T >

The QLinkedList is a template class which stores values in a linked list. This container is implemented using a doubly linked list which means adding or removing elements are fast anywhere in the linked list.

  • Operations like prepend() and insert() are usually faster using QList than QVector
  • When elements need to occupy adjacent memory locations, to support a C style API, use QVector
  • If you want a low level variable size array, QVarLengthArray may be sufficient.
  • QVector and QList support access by index, for a QLinkedList iterators must be used

For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.

Constructors

The following code is a simple example showing how to declare a QLinkedList.

QLinkedList<int> intList; // stores int values
QLinkedList<QTime> timeList; // stores QTime values

Basic Operations

To insert items into a QLinkedList the operator<<() method can be used.

list << "one" << "two" << "three"; // list: ["one", "two", "three"]

To get the first or last item in a linked list use first() or last(). If you want to remove an item from either end of the list, use removeFirst() or removeLast(). If you want to remove all occurrences of a given value in the list, use removeAll().

A common requirement is to remove the first or last item in the list and do something with it. QLinkedList provides takeFirst() and takeLast().

The following is a loop which removes the items from a list one at a time and calls delete on them.

while (! list.isEmpty()) {
delete list.takeFirst();
}

Constraints on type T

The QVector value type T should be Default Constructible and Copy Constructible to work with all methods of this class. Data types like QObject are not copy constructible and therefore should never be stored in a container. You can however use a pointer to a QObject or any subclass as the type T.

To insert, modify, or remove items in the middle of the list use an iterator. QLinkedList provides both Java style iterators (QLinkedListIterator and QMutableLinkedListIterator) and STL style iterators (QLinkedList::const_iterator and QLinkedList::iterator).

See also
QList, QVector

Member Typedef Documentation

template<class T >
QLinkedList< T >::allocator_type

Typedef for allocator used by the container.

template<class T >
QLinkedList< T >::const_iterator

Typedef for std::list<T>::const_iterator.

template<class T >
QLinkedList< T >::const_pointer

Typedef for const T *.

template<class T >
QLinkedList< T >::const_reference

Typedef for const T &.

template<class T >
QLinkedList< T >::const_reverse_iterator

Typedef for an STL style const reverse iterator.

template<class T >
QLinkedList< T >::difference_type

Typedef for ptrdiff_t.

template<class T >
QLinkedList< T >::iterator

Typedef for std::list<T>::iterator.

template<class T >
QLinkedList< T >::Java_Iterator

Typedef for the java style const iterator.

See also
QSetIterator()
template<class T >
QLinkedList< T >::Java_MutableIterator

Typedef for the java style mutable iterator.

See also
QMutableSetIterator()
template<class T >
QLinkedList< T >::pointer

Typedef for T *.

template<class T >
QLinkedList< T >::reference

Typedef for T &.

template<class T >
QLinkedList< T >::reverse_iterator

Typedef for an STL style reverse iterator.

template<class T >
QLinkedList< T >::size_type

Typedef for int.

template<class T >
QLinkedList< T >::value_type

Typedef for T.

Constructor & Destructor Documentation

template<class T >
QLinkedList< T >::QLinkedList ( )
default

Constructs an empty list.

template<class T >
QLinkedList< T >::QLinkedList ( const QLinkedList< T > &  other)
default

Copy constructs a new QLinkList from other.

template<class T >
QLinkedList< T >::QLinkedList ( QLinkedList< T > &&  other)
default

Move constructs a new QLinkList from other.

template<class T >
QLinkedList< T >::QLinkedList ( std::initializer_list< T >  args)
inline

Construct a linked list from the std::initializer_list specified by args.

template<class T >
template<class Input_Iterator >
QLinkedList< T >::QLinkedList ( Input_Iterator  first,
Input_Iterator  last 
)
inline

Construct a linked list containing the elements from first to last.

template<class T >
QLinkedList< T >::~QLinkedList ( )
default

Destroys the QLinkedList.

Method Documentation

template<class T >
void QLinkedList< T >::append ( const QLinkedList< T > &  other)
inline

Copy appends the items of value to this QLinkedList.

template<class T >
void QLinkedList< T >::append ( const T &  value)
inline

Copy append value at the end of the list.

list.append("one");
list.append("two");
list.append("three"); // list: ["one", "two", "three"]

Equivalent to calling list.insert(end(), value).

See also
operator<<(), prepend(), insert()
template<class T >
void QLinkedList< T >::append ( T &&  value)
inline

Move appends from value to this QLinkedList.

template<class T >
reference QLinkedList< T >::back ( )
inline

Returns a reference to the last element. Equivalent to calling last().

template<class T >
const_reference QLinkedList< T >::back ( ) const
inline

Returns a const reference to the last element. Equivalent to calling last().

template<class T >
iterator QLinkedList< T >::begin ( )
inline

Returns an STL style iterator pointing to the first item in the list.

See also
constBegin(), end()
template<class T >
const_iterator QLinkedList< T >::begin ( ) const
inline

Returns a const STL style iterator pointing to the first item in the list.

template<class T >
const_iterator QLinkedList< T >::cbegin ( ) const
inline

Returns a const STL style iterator positioned at the first item in the set.

See also
cend()
template<class T >
const_iterator QLinkedList< T >::cend ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the set.

See also
cbegin()
template<class T >
void QLinkedList< T >::clear ( )
inline

Removes all the items in the list.

See also
removeAll()
template<class T >
const_iterator QLinkedList< T >::constBegin ( ) const
inline

Returns a const STL style iterator pointing to the first item in the list.

See also
constEnd()
template<class T >
const_iterator QLinkedList< T >::constEnd ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the list.

See also
constBegin()
template<typename T >
bool QLinkedList< T >::contains ( const T &  value) const

Returns true if the list contains an occurrence of value, otherwise returns false. This method requires the type T to support operator==().

See also
QLinkedListIterator::findNext(), QLinkedListIterator::findPrevious()
template<class T >
size_type QLinkedList< T >::count ( ) const
inline

Equivalent to calling size().

template<typename T >
QLinkedList< T >::size_type QLinkedList< T >::count ( const T &  value) const

Returns the number of occurrences of value in the list. This method requires the type T to support operator==().

See also
contains()
template<class T >
const_reverse_iterator QLinkedList< T >::crbegin ( ) const
inline

Returns a const STL style reverse iterator pointing to the first item in the list, in reverse order.

template<class T >
const_reverse_iterator QLinkedList< T >::crend ( ) const
inline

Returns a const STL style reverse iterator pointing to one past the last item in the list, in reverse order.

template<class T >
bool QLinkedList< T >::empty ( ) const
inline

Equivalent to calling isEmpty().

template<class T >
iterator QLinkedList< T >::end ( )
inline

Returns an STL style iterator pointing to the imaginary item after the last item in the list.

See also
begin(), constEnd()
template<class T >
const_iterator QLinkedList< T >::end ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the list.

template<class T >
bool QLinkedList< T >::endsWith ( const T &  value) const
inline

Returns true if the list is not empty and its last item is equal to value, otherwise returns false.

See also
isEmpty(), last()
template<class T >
iterator QLinkedList< T >::erase ( const_iterator  begin,
const_iterator  end 
)
inline

Removes all the items from begin up to (but not including) end.

template<class T >
iterator QLinkedList< T >::erase ( const_iterator  pos)
inline

Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).

See also
insert()
template<class T >
reference QLinkedList< T >::first ( )
inline

Returns a reference to the first item in the list. This function assumes that the list is not empty.

See also
last(), isEmpty()
template<class T >
const_reference QLinkedList< T >::first ( ) const
inline

This is an overloaded method.

template<class T >
QLinkedList< T > QLinkedList< T >::fromStdList ( const std::list< T > &  other)
inlinestatic

Returns a QLinkedList object with the data contained in other. The order of the elements in the QLinkedList is the same as in list.

std::list<double> other;
other.push_back(1.2);
other.push_back(0.5);
other.push_back(3.14);
See also
toStdList()
template<class T >
reference QLinkedList< T >::front ( )
inline

Equivalent to calling first().

template<class T >
const_reference QLinkedList< T >::front ( ) const
inline

Equivalent to calling first().

template<class T >
iterator QLinkedList< T >::insert ( iterator  before,
const T &  value 
)
inline

Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.

See also
erase()
template<class T >
iterator QLinkedList< T >::insert ( iterator  before,
size_type  count,
const T &  value 
)
inline

Inserts count copies of value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.

See also
erase()
template<class T >
bool QLinkedList< T >::isEmpty ( ) const
inline

Returns true if the list contains no items, otherwise returns false.

See also
size()
template<class T >
reference QLinkedList< T >::last ( )
inline

Returns a reference to the last item in the list. This function assumes that the list is not empty.

See also
first(), isEmpty()
template<class T >
const_reference QLinkedList< T >::last ( ) const
inline

This is an overloaded method.

template<class T >
size_type QLinkedList< T >::length ( ) const
inline

Equivalent to calling size().

template<class T >
bool QLinkedList< T >::operator!= ( const QLinkedList< T > &  other) const
inline

Returns true if other is not equal to this list, otherwise returns false. Two lists are considered equal if they contain the same values in the same order.

This method requires the type T to support operator==().

See also
operator==()
template<class T >
QLinkedList< T > QLinkedList< T >::operator+ ( const QLinkedList< T > &  other) const
inline

Returns a list that contains all the items in this list followed by all the items in the other list.

See also
operator+=()
template<typename T >
QLinkedList< T > & QLinkedList< T >::operator+= ( const QLinkedList< T > &  other)

Appends the items of the other list to this list and returns a reference to this list.

See also
operator+(), append()
template<class T >
QLinkedList< T > & QLinkedList< T >::operator+= ( const T &  value)
inline

Appends value to the list.

template<class T >
QLinkedList< T > & QLinkedList< T >::operator<< ( const QLinkedList< T > &  other)
inline

Appends the items of the other list to this list and returns a reference to this list.

See also
operator+=(), append()
template<class T >
QLinkedList< T > & QLinkedList< T >::operator<< ( const T &  value)
inline

Appends value to the list.

template<class T >
QLinkedList< T > & QLinkedList< T >::operator= ( const QLinkedList< T > &  other)
default

Copy assigns from other and returns a reference to this object.

template<class T >
QLinkedList< T > & QLinkedList< T >::operator= ( QLinkedList< T > &&  other)
default

Move assigns from other and returns a reference to this object.

This version takes an rvalue. Refer to references for a detailed description of rvalues and move semantics.

template<class T >
bool QLinkedList< T >::operator== ( const QLinkedList< T > &  other) const
inline

Returns true if other is equal to this list, otherwise returns false. Two lists are considered equal if they contain the same values in the same order.

This method requires the type T to support operator==().

See also
operator!=()
template<class T >
void QLinkedList< T >::pop_back ( )
inline

Equivalent to calling removeLast().

template<class T >
void QLinkedList< T >::pop_front ( )
inline

Equivalent to calling removeFirst().

template<class T >
void QLinkedList< T >::prepend ( const T &  value)
inline

Inserts value at the beginning of the list.

list.prepend("one");
list.prepend("two");
list.prepend("three"); // list: ["three", "two", "one"]

Equivalent to calling list.insert(begin(), value).

See also
append(), insert()
template<class T >
void QLinkedList< T >::push_back ( const T &  value)
inline

Equivalent to calling append(value).

template<class T >
void QLinkedList< T >::push_front ( const T &  value)
inline

Equivalent to calling prepend(value).

template<class T >
reverse_iterator QLinkedList< T >::rbegin ( )
inline

Returns a const STL style reverse iterator pointing to the first item in the list, in reverse order.

template<class T >
const_reverse_iterator QLinkedList< T >::rbegin ( ) const
inline

This is an overloaded method.

template<typename T >
QLinkedList< T >::size_type QLinkedList< T >::removeAll ( const T &  value)

Removes all occurrences of value in the list.

list << "sun" << "cloud" << "sun" << "rain";
list.removeAll("sun");
// list: ["cloud", "rain"]

This method requires the type T to support operator==().

See also
insert()
template<class T >
void QLinkedList< T >::removeFirst ( )
inline

Removes the first item in the list. Equivalent to calling erase(begin()).

See also
removeLast(), erase()
template<class T >
void QLinkedList< T >::removeLast ( )
inline

Removes the last item in the list.

See also
removeFirst(), erase()
template<typename T >
bool QLinkedList< T >::removeOne ( const T &  value)

Removes the first occurrences of value in the list. Returns true on success, otherwise returns false.

list << "sun" << "cloud" << "sun" << "rain";
list.removeOne("sun");
// list: ["cloud", "sun", "rain"]

This method requires the type T to support operator==().

See also
insert()
template<class T >
reverse_iterator QLinkedList< T >::rend ( )
inline

Returns a const STL style reverse iterator pointing to one past the last item in the list, in reverse order.

template<class T >
const_reverse_iterator QLinkedList< T >::rend ( ) const
inline

This is an overloaded method.

template<class T >
size_type QLinkedList< T >::size ( ) const
inline

Returns the number of items in the list.

See also
isEmpty(), count()
template<class T >
bool QLinkedList< T >::startsWith ( const T &  value) const
inline

Returns true if the list is not empty and its first item is equal to value, otherwise returns false.

See also
isEmpty(), first()
template<class T >
void QLinkedList< T >::swap ( QLinkedList< T > &  other)
inline

Swaps list other with this list. This operation is very fast and never fails.

template<class T >
T QLinkedList< T >::takeFirst ( )
inline

Removes the first item in the list and returns it.

If you do not use the return value, removeFirst() is more efficient.

See also
takeLast(), removeFirst()
template<class T >
T QLinkedList< T >::takeLast ( )
inline

Removes the last item in the list and returns it.

If you do not use the return value, removeLast() is more efficient.

See also
takeFirst(), removeLast()
template<class T >
std::list< T > QLinkedList< T >::toStdList ( ) const
inline

Returns a std::list object with the data contained in this QLinkedList.

list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();
See also
fromStdList()

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream out,
const QLinkedList< T > &  list 
)
related

Writes the linked list list to stream out. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream in,
QLinkedList< T > &  list 
)
related

Reads a linked list from stream in into list. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.