CopperSpice API  1.9.2
QList< T > Class Template Reference

The QList class is a template class which stores a list of values. More...

Inheritance diagram for QList< T >:
QQueue< T >

Classes

class  const_iterator
 The QList::const_iterator class provides an STL style const iterator for QList and QQueue More...
 
class  iterator
 The QList::iterator class provides an STL style iterator for QList and QQueue More...
 

Public Typedefs

using allocator_type = typename std::deque< T >::allocator_type
 
using const_iterator = typename std::deque< T >::const_iterator
 
using const_pointer = typename std::deque< T >::const_pointer
 
using const_reference = typename std::deque< T >::const_reference
 
using const_reverse_iterator = typename std::deque< T >::const_reverse_iterator
 
using difference_type = typename std::deque< T >::difference_type
 
using iterator = typename std::deque< T >::iterator
 
using Java_Iterator = QListIterator< T >
 
using Java_MutableIterator = QMutableListIterator< T >
 
using pointer = typename std::deque< T >::pointer
 
using reference = typename std::deque< T >::reference
 
using reverse_iterator = typename std::deque< T >::reverse_iterator
 
using size_type = typename std::deque< T >::difference_type
 
using value_type = typename std::deque< T >::value_type
 

Public Methods

 QList () = default
 
 QList (const QList< T > &other) = default
 
template<class Input_Iterator >
 QList (Input_Iterator first, Input_Iterator last)
 
 QList (QList< T > &&other) = default
 
 QList (std::initializer_list< T > args)
 
 ~QList () = default
 
void append (const QList< T > &other)
 
void append (const T &value)
 
void append (QList< T > &&other)
 
void append (T &&value)
 
const T & at (size_type i) const
 
T & back ()
 
const T & 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
 
const_reference constFirst () const
 
const_reference constLast () 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)
 
T & first ()
 
const T & first () const
 
T & front ()
 
const T & front () const
 
size_type indexOf (const T &value, size_type from=0) const
 
iterator insert (iterator before, const T &value)
 
void insert (size_type i, const T &value)
 
bool isEmpty () const
 
T & last ()
 
const T & last () const
 
size_type lastIndexOf (const T &value, size_type from=-1) const
 
size_type length () const
 
QList< T > mid (size_type pos, size_type length=-1) const
 
void move (size_type from, size_type to)
 
bool operator!= (const QList< T > &other) const
 
QList< T > operator+ (const QList< T > &other) const
 
QList< T > & operator+= (const QList< T > &other)
 
QList< T > & operator+= (const T &value)
 
QList< T > & operator<< (const QList< T > &other)
 
QList< T > & operator<< (const T &value)
 
QList< T > & operator= (const QList< T > &other) = default
 
QList< T > & operator= (QList< T > &&other) = default
 
bool operator== (const QList< T > &other) const
 
T & operator[] (size_type i)
 
const T & operator[] (size_type i) 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 removeAt (size_type i)
 
void removeFirst ()
 
void removeLast ()
 
bool removeOne (const T &value)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
void replace (size_type i, const T &value)
 
void resize (size_type size)
 
size_type size () const
 
bool startsWith (const T &value) const
 
void swap (QList< T > &other)
 
void swap (size_type i, size_type j)
 
takeAt (size_type i)
 
takeFirst ()
 
takeLast ()
 
QSet< T > toSet () const
 
std::list< T > toStdList () const
 
QVector< T > toVector () const
 
value (size_type i) const
 
value (size_type i, const T &defaultValue) const
 

Static Public Methods

static QList< T > fromSet (const QSet< T > &set)
 
static QList< T > fromStdList (const std::list< T > &other)
 
static QList< T > fromVector (const QVector< T > &vector)
 

Related Functions

These are not member functions

bool operator< (const QList< T > &lhs, const QList< T > &rhs)
 
QDataStreamoperator<< (QDataStream &stream, const QList< T > &list)
 
bool operator<= (const QList< T > &lhs, const QList< T > &rhs)
 
bool operator> (const QList< T > &lhs, const QList< T > &rhs)
 
bool operator>= (const QList< T > &lhs, const QList< T > &rhs)
 
QDataStreamoperator>> (QDataStream &stream, QList< T > &list)
 

Detailed Description

template<typename T>
class QList< T >

The QList class is a template class which stores a list of values. This container stores elements in large blocks of memory which provides fast index access. Insert and erase at the beginning or end of the container will be fast, whereas in the middle may be slow.

The QStringList class which inherits from QList<QString> adds several methods which pertain specifically to working with a list of strings.

  • 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

A QVector can be created with an initial size. The following code constructs a QList with 200 elements and each element will be default initialized. Refer to default constructed elements for additional information.

QList<QString> list(200);

Accessing Elements

QList, like all other containers in C++, uses 0 based indexes. To access the item at a particular index position you can use operator[]. For a non-const list operator[] returns an lvalue reference to the item and can be used on the left side of an assignment.

if (list[0] == "Bob") {
list[0] = "Robert";
}

For read only access an alternative syntax is to use the at() method.

for (int i = 0; i < list.size(); ++i) {
if (list.at(i) == "Jane") {
cout << "Found Jane at position " << i << endl;
}
}

Basic Operations

QList provides basic methods to add, move, and remove items: insert(), replace(), removeAt(), move(), and swap(). This class also has methods to append(), prepend(), removeFirst(), and removeLast().

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

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

A common requirement is to remove an item from a list and do something with it. QList provides takeAt(), takeFirst(), and takeLast(). The following is a loop which removes the items from a list one at a time and calls delete.

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

Inserting and removing items at either ends of the list is fast because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.

If you want to find all occurrences of a particular value in a list, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of a matching item if they find it, otherwise they return -1.

int i = list.indexOf("Jane");
if (i != -1) {
cout << "First occurrence of Jane is at position " << i << endl;
}

If you simply want to check whether a list contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the list, use count(). If you want to replace all occurrences of a particular value with another, use replace().

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.

A few methods have additional requirements. For example, indexOf() and lastIndexOf() need the value type T to support operator==(). If any other constraints exist, they will appear in the documentation for the given method.

QList provides Java style iterators (QListIterator and QMutableListIterator) and STL style iterators (QList::const_iterator and QList::iterator). As an alternative you can use indexes into a QList however they are slightly slower than using an iterator.

Additional Information

QList does not support inserting, prepending, appending, or replacing with references to its own values. Doing so may cause your application to abort with an error message. All methods except isEmpty() assume the current QList is not empty. Methods with a parameter of an index value always assume the index is in the valid range of elements.

If you define CS_DISABLE_ASSERT failures will not be detected or reported. Errors may produce undefined behavior.

See also
QLinkedList, QVector

Member Typedef Documentation

template<typename T >
QList< T >::allocator_type

Typedef for allocator used by the container.

template<typename T >
QList< T >::const_iterator

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

template<typename T >
QList< T >::const_pointer

Typedef for const T *.

template<typename T >
QList< T >::const_reference

Typedef for const T &.

template<typename T >
QList< T >::const_reverse_iterator

Typedef for an STL style const reverse iterator.

template<typename T >
QList< T >::difference_type

Typedef for ptrdiff_t.

template<typename T >
QList< T >::iterator

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

template<typename T >
QList< T >::Java_Iterator

Typedef for the java style const iterator.

See also
QSetIterator()
template<typename T >
QList< T >::Java_MutableIterator

Typedef for the java style mutable iterator.

See also
QMutableSetIterator()
template<typename T >
QList< T >::pointer

Typedef for T *.

template<typename T >
QList< T >::reference

Typedef for T &.

template<typename T >
QList< T >::reverse_iterator

Typedef for an STL style reverse iterator.

template<typename T >
QList< T >::size_type

Typedef for a signed integer of the appropriate size for your platform.

template<typename T >
QList< T >::value_type

Typedef for T.

Constructor & Destructor Documentation

template<typename T >
QList< T >::QList ( )
default

Constructs an empty list.

template<typename T >
QList< T >::QList ( const QList< T > &  other)
default

Copy constructs a new QList from other.

template<typename T >
QList< T >::QList ( QList< T > &&  other)
default

Move constructs a new QList from other.

template<typename T >
QList< T >::QList ( std::initializer_list< T >  args)
inline

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

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

Construct a list containing the elements fromfirst to last.

template<typename T >
QList< T >::~QList ( )
default

Destroys this QList.

Method Documentation

template<typename T >
void QList< T >::append ( const QList< T > &  other)
inline

Copy appends the items of other to this QList.

See also
operator<<(), operator+=()
template<typename T >
void QList< T >::append ( const T &  value)
inline

Appends value at the end of the list.

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

This is the same as list.insert(size(), value).

This operation is typically very fast (constant time) because QList preallocates extra space on both sides of the internal buffer to allow for fast growth at both ends.

See also
operator<<(), prepend(), insert()
template<typename T >
void QList< T >::append ( QList< T > &&  other)
inline

Move appends the items of other to this QList.

See also
operator<<(), operator+=()
template<typename T >
void QList< T >::append ( T &&  value)
inline

Move appends from value to this QList.

template<typename T >
const T & QList< T >::at ( size_type  i) const
inline

Returns the item at index position i in the list. The value for i must be a valid index position in the list, i >= 0 and i < size().

This method is very fast. Refer to constant time for more information.

See also
value(), operator[]()
template<typename T >
T & QList< T >::back ( )
inline

Equivalent to calling last(). Verify the list is not empty before calling this method.

template<typename T >
const T & QList< T >::back ( ) const
inline

Equivalent to calling last(). Verify the list is not empty before calling this method.

template<typename T >
iterator QList< T >::begin ( )
inline

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

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

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

template<typename T >
const_iterator QList< T >::cbegin ( ) const
inline

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

See also
cend()
template<typename T >
const_iterator QList< 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<typename T >
void QList< T >::clear ( )
inline

Removes all items from the list.

See also
removeAll()
template<typename T >
const_iterator QList< T >::constBegin ( ) const
inline

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

See also
constEnd()
template<typename T >
const_iterator QList< 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 >
const_reference QList< T >::constFirst ( ) const
inline

Returns a const reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
constLast(), isEmpty(), first()
template<typename T >
const_reference QList< T >::constLast ( ) const
inline

Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
constFirst(), isEmpty(), last()
template<typename T >
bool QList< T >::contains ( const T &  value) const

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

See also
indexOf(), count()
template<typename T >
size_type QList< T >::count ( ) const
inline

Returns the number of items in the list. This is effectively the same as size().

template<typename T >
QList< T >::size_type QList< 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(), indexOf()
template<typename T >
const_reverse_iterator QList< T >::crbegin ( ) const
inline

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

template<typename T >
const_reverse_iterator QList< 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<typename T >
bool QList< T >::empty ( ) const
inline

Equivalent to calling isEmpty().

template<typename T >
iterator QList< 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<typename T >
const_iterator QList< T >::end ( ) const
inline

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

template<typename T >
bool QList< T >::endsWith ( const T &  value) const
inline

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

See also
isEmpty(), contains()
template<typename T >
iterator QList< T >::erase ( const_iterator  begin,
const_iterator  end 
)
inline

Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.

template<typename T >
iterator QList< T >::erase ( const_iterator  pos)
inline

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

See also
insert(), removeAt()
template<typename T >
T & QList< T >::first ( )
inline

Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
last(), isEmpty()
template<typename T >
const T & QList< T >::first ( ) const
inline

Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

template<typename T >
QList< T > QList< T >::fromSet ( const QSet< T > &  set)
static

Returns a QList object with the data contained in set. The order of the elements in the QList is undefined.

set << 20 << 30 << 40 << ... << 70;
std::sort(list.begin(), list.end());
See also
fromVector(), toSet(), QSet::toList()
template<typename T >
QList< T > QList< T >::fromStdList ( const std::list< T > &  other)
inlinestatic

Returns a QList containing copies of the items in the given other.

std::list<double> other;
other.push_back(1.2);
other.push_back(0.5);
other.push_back(3.14);
See also
toStdList(), QVector::fromStdVector()
template<typename T >
QList< T > QList< T >::fromVector ( const QVector< T > &  vector)
static

Returns a QList object with the data contained in vector.

vect << 20.0 << 30.0 << 40.0 << 50.0;
// list: [20.0, 30.0, 40.0, 50.0]
See also
fromSet(), toVector(), QVector::toList()
template<typename T >
T & QList< T >::front ( )
inline

Equivalent to calling first(). Verify the list is not empty before calling this method.

template<typename T >
const T & QList< T >::front ( ) const
inline

Equivalent to calling first(). Verify the list is not empty before calling this method.

template<typename T >
size_type QList< T >::indexOf ( const T &  value,
size_type  from = 0 
) const
inline

Returns the index position of the first occurrence of value in the list, searching forward from index position from. Returns -1 if no item matched.

list << "A" << "B" << "C" << "B" << "A";
list.indexOf("B"); // returns 1
list.indexOf("B", 1); // returns 1
list.indexOf("B", 2); // returns 3
list.indexOf("X"); // returns -1

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

See also
lastIndexOf(), contains()
template<typename T >
iterator QList< 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. The iterator passed to the method will be invalid after the call; the returned iterator should be used instead.

template<typename T >
void QList< T >::insert ( size_type  i,
const T &  value 
)
inline

Inserts value at index position i in the list. If i is 0, the value is prepended to the list. If i is size(), the value is appended to the list.

list << "alpha" << "beta" << "delta";
list.insert(2, "gamma"); // list: ["alpha", "beta", "gamma", "delta"]
See also
append(), prepend(), replace(), removeAt()
template<typename T >
bool QList< T >::isEmpty ( ) const
inline

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

See also
size()
template<typename T >
T & QList< T >::last ( )
inline

Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

See also
first(), isEmpty()
template<typename T >
const T & QList< T >::last ( ) const
inline

Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this method.

template<typename T >
size_type QList< T >::lastIndexOf ( const T &  value,
size_type  from = -1 
) const
inline

Returns the index position of the last occurrence of value in the list, searching backward from index position from. If from is -1 the search starts at the last item. Returns -1 if no item matched.

list << "A" << "B" << "C" << "B" << "A";
list.lastIndexOf("B"); // returns 3
list.lastIndexOf("B", 3); // returns 3
list.lastIndexOf("B", 2); // returns 1
list.lastIndexOf("X"); // returns -1

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

See also
indexOf()
template<typename T >
size_type QList< T >::length ( ) const
inline

This method is identical to count().

See also
count()
template<typename T >
QList< T > QList< T >::mid ( size_type  pos,
size_type  length = -1 
) const

Returns a list whose elements are copied from this list, starting at position pos. If length is -1 then all elements after pos are returned, otherwise the given length of elements is returned. If the length is greater than number of elements the value for length is ignored.

template<typename T >
void QList< T >::move ( size_type  from,
size_type  to 
)
inline

Moves the item at index position from to index position to.

list << "A" << "B" << "C" << "D" << "E" << "F";
list.move(1, 4); // list: ["A", "C", "D", "E", "B", "F"]

Equivalent to calling insert(to, takeAt(from)). This method assumes both "from" and "to" are valid indexes.

See also
swap(), insert(), takeAt()
template<typename T >
bool QList< T >::operator!= ( const QList< 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<typename T >
QList< T > QList< T >::operator+ ( const QList< 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 >
QList< T > & QList< T >::operator+= ( const QList< 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<typename T >
QList< T > & QList< T >::operator+= ( const T &  value)
inline

Appends value to the list.

See also
append(), operator<<()
template<typename T >
QList< T > & QList< T >::operator<< ( const QList< 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<typename T >
QList< T > & QList< T >::operator<< ( const T &  value)
inline

Appends value to the list.

template<typename T >
QList< T > & QList< T >::operator= ( const QList< T > &  other)
default

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

template<typename T >
QList< T > & QList< T >::operator= ( QList< 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<typename T >
bool QList< T >::operator== ( const QList< 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<typename T >
T & QList< T >::operator[] ( size_type  i)
inline

Returns a reference to the item at index position i. The value i must be a valid index position in the list.

See also
at(), value()
template<typename T >
const T & QList< T >::operator[] ( size_type  i) const
inline

Equivalent to calling at().

template<typename T >
void QList< T >::pop_back ( )
inline

Equivalent to calling removeLast(). Verify the list is not empty before calling this method.

template<typename T >
void QList< T >::pop_front ( )
inline

Equivalent to calling removeFirst(). Verify the list is not empty before calling this method.

template<typename T >
void QList< T >::prepend ( const T &  value)
inline

Inserts value at the beginning of the list. Equivalent to calling list.insert(0, value).

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

This operation is usually very fast (constant time) because QList preallocates extra space on both sides of the internal buffer to allow for fast growth at both ends.

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

Equivalent to calling append(value).

template<typename T >
void QList< T >::push_front ( const T &  value)
inline

Equivalent to calling prepend(value).

template<typename T >
reverse_iterator QList< T >::rbegin ( )
inline

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

template<typename T >
const_reverse_iterator QList< T >::rbegin ( ) const
inline

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

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

Removes all occurrences of value in the list and returns the number of entries removed.

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

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

See also
removeOne(), removeAt(), takeAt(), replace()
template<typename T >
void QList< T >::removeAt ( size_type  i)
inline

Removes the item at index position i. The value i must be a valid index position in the list.

See also
takeAt(), removeFirst(), removeLast(), removeOne()
template<typename T >
void QList< T >::removeFirst ( )
inline

Equivalent to calling removeAt(0). Verify the list is not empty before calling this method.

See also
removeAt(), takeFirst()
template<typename T >
void QList< T >::removeLast ( )
inline

Equivalent to calling removeAt(size() - 1). Verify the list is not empty before calling this method.

See also
removeAt(), takeLast()
template<typename T >
bool QList< T >::removeOne ( const T &  value)

Removes the first occurrence of value in the list and returns true on success, otherwise returns false.

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

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

See also
removeAll(), removeAt(), takeAt(), replace()
template<typename T >
reverse_iterator QList< T >::rend ( )
inline

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

template<typename T >
const_reverse_iterator QList< T >::rend ( ) const
inline

This is an overloaded method.

template<typename T >
void QList< T >::replace ( size_type  i,
const T &  value 
)
inline

Replaces the item at index position i with value. The value for i must be a valid index position in the list, i >= 0 and i < size().

See also
operator[](), removeAt()
template<typename T >
void QList< T >::resize ( size_type  size)
inline

Resize this list to contain size number of elements.

template<typename T >
size_type QList< T >::size ( ) const
inline

Returns the number of items in the list.

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

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

See also
isEmpty(), contains()
template<typename T >
void QList< T >::swap ( QList< T > &  other)
inline

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

template<typename T >
void QList< T >::swap ( size_type  i,
size_type  j 
)
inline

Exchange the item at index position i with the item at index position j. This method assumes that both i and j are at least 0 but less than size(). To avoid failure, test that both i and j are at least 0 and less than size().

list << "A" << "B" << "C" << "D" << "E" << "F";
list.swap(1, 4);
// list: ["A", "E", "C", "D", "B", "F"]
See also
move()
template<typename T >
T QList< T >::takeAt ( size_type  i)
inline

Removes the item at index position i and returns it. The value for i must be a valid index position in the list, i >= 0 and i < size(). If the return value is not used then calling removeAt() is more efficient.

See also
removeAt(), takeFirst(), takeLast()
template<typename T >
T QList< T >::takeFirst ( )
inline

Removes the first item in the list and returns it. This is the same as takeAt(0). This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.

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

See also
takeLast(), takeAt(), removeFirst()
template<typename T >
T QList< T >::takeLast ( )
inline

Removes the last item in the list and returns it. This is the same as takeAt(size() - 1). This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.

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

See also
takeFirst(), takeAt(), removeLast()
template<typename T >
QSet< T > QList< T >::toSet ( ) const

Returns a QSet object with the data contained in this QList. Since QSet does not allow duplicates, the resulting QSet might be smaller than the original list.

list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia";
QSet<QString> set = list.toSet();
set.contains("Julia"); // returns true
set.contains("Mike"); // returns true
set.size(); // returns 2
See also
toVector(), fromSet(), QSet::fromList()
template<typename T >
std::list< T > QList< T >::toStdList ( ) const
inline

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

list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();
See also
fromStdList(), QVector::toStdVector()
template<typename T >
QVector< T > QList< T >::toVector ( ) const

Returns a QVector object with the data contained in this QList.

list << "Sven" << "Kim" << "Ola";
QVector<QString> vect = list.toVector();
// vect: ["Sven", "Kim", "Ola"]
See also
toSet(), fromVector(), QVector::fromList()
template<typename T >
T QList< T >::value ( size_type  i) const

Returns the value at index position i in the list. If you are certain the index is going to be within bounds, use the method at() which is slightly faster. If the index is out of bounds, a value initialized object is returned. Refer to default constructed elements for additional information.

See also
at(), operator[]()
template<typename T >
T QList< T >::value ( size_type  i,
const T &  defaultValue 
) const

Returns the value at index position i in the list. If the index i is out of bounds defaultValue is returned.

Friends And Related Function Documentation

bool operator< ( const QList< T > &  lhs,
const QList< T > &  rhs 
)
related

Returns true if list lhs is lexicographically less than rhs, otherwise returns false. This method requires the type T to support operator<().

QDataStream & operator<< ( QDataStream stream,
const QList< T > &  list 
)
related

Writes the given list to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

bool operator<= ( const QList< T > &  lhs,
const QList< T > &  rhs 
)
related

Returns true if list lhs is lexicographically less than or equal to rhs, otherwise returns false. This method requires the type T to support operator<().

bool operator> ( const QList< T > &  lhs,
const QList< T > &  rhs 
)
related

Returns true if list lhs is lexicographically greater than rhs, otherwise returns false. This method requires the type T to support operator<().

bool operator>= ( const QList< T > &  lhs,
const QList< T > &  rhs 
)
related

Returns true if list lhs is lexicographically greater than or equal to rhs, otherwise returns false. This method requires the type T to support operator<().

QDataStream & operator>> ( QDataStream stream,
QList< T > &  list 
)
related

Reads from the stream into the given list. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.