The QValueList class is a value based template class that provides doubly linked lists. More...
#include <qvaluelist.h>
Define a template instance QValueList<X> to create a list of values which all have the class X. Please notice that QValueList does not store pointers to the members of the list. It holds a copy of every member. That is the reason why this kind of classes are called "value based" while QList and QDict are "reference based".
Some classes can not be used within a QValueList. For example everything derived from QObject and as a reason of this that extends to all classes which implement widgets. You can only put stuff in a QValueList which can be easily copied. That means that the class X should implement a fast copy constructor.
Example:
#include <qvaluelist.h> #include <qstring.h> #include <stdio.h> class Employee { public: Employee( const QString& name, int salary ) { n=name; s=salary; } Employee( const Employee& _e ) { n = _e.name; s = _e.s; } QString name() const { return n; } int salary() const { return s; } void setSalary( int _s ) const { s = _s; } private: QString n; int s; }; void main() { typedef QValueList<Employee> EmployeeList; EmployeeList list; // list of Employee list.append( Employee("Bill", 50000) ); list.append( Employee("Steve",80000) ); list.append( Employee("Ron", 60000) ); Employee joe( "Joe", 50000 ); list.append( joe ); joe.setSalary( 4000 ); EmployeeList::Iterator it; for( it = list.begin(); it != list.end(); ++it ) printf( "%s earns %d\n", (*it).name(), (*it).salary() ); }
Program output:
Bill earns 50000 Steve earns 80000 Ron earns 60000 Joe earns 50000
As you can see, the latest changes to Joes salary did not affect the value in the list because the list created a copy of Joes entry.
There are three ways of finding items in the list. The first one is by using the at() function. It returns an iterator. The advantages of getting an iterator is that you can now move forward or backward from this position by incrementing/decrementing the iterator. To get the amount of items in the list call count(). Valid indices are 0..count().
The second way of accessing a list is with operator[]. That means you can address it like an array. The return value is a reference to the value stored in the list. There exist two versions of this operator. The first one is const and returns a const reference to the value. The second on is non const and returns a non const reference to the value. It is up to your compiler to choose the correct one.
The third method is to use the functions begin() and end(). With a simple for loop as shown in the example you can iterate over the complete list. It is save to have multiple iterators at the same time. If some member of the list is removed then only iterators pointing to the removed member become invalid. Inserting in the list does not invalidate any iterator. For convenience the function last() returns an iterator for the last and first() for thge first element in the list.
In addition you can search items in the list with the find() function. It exists in a const and a non const version. It starts searching from the beginning of the list, but another flavour of the find() function allows you to specify where searching should start. If you just want to know wether a certain item is at least once in the list, then you can use the contains() function.
Since QValueList is value based there is no need to care about deleting elements in the list. The list holds its own copies and will free them if the corresponding member or the list itself is deleted. You can force the list to free all of its item with clear().
QValueList is implict shared. That means you can just make copies of the list in time O(1). If multiple QValueList instances share the same data and one is doing a modification of the lists data then this modifying instance makes a copy and modifies its private copy. So it does not affect the other instances. From a developers point of view you can think that a QValueList and a copy of this list have nothing to do with each other. Developers may only notice that copying is very fast. People known to a CPUs MMU architecture will know this pattern as "copy on write".
There exist three functions to insert items in the list. append() inserts an item at the end, prepend() inserts at the beginning and insert() inserts in front of the position given by an iterator.
Items can be removed from the list in two ways. The first is to pass an iterator to the remove(). The other possibility is to pass a value to remove() which will delete all members which match this value.
Sorting of the list is not supported by the class itself. That has three reasons: For one Qt does not require that the class of the instances stored in a QValueList do need to implement operator<. Reason number three is that sorting is very often not needed. By adding the sorting algorithm to the QValueList template the compiler would generate sorting code for every incarnation of the template but it is never used. Reason number three is that you can choose between different sorting algorithms. TODO: Reference to QTL
Example:
QValueList l; l.append( 5 ); l.append( 8 ); l.append( 3 ); l.append( 4 ); qHeapSort( l );
See also: QValueListIterator.
Constructs an empty list.
Destroys the list. References to the values in the list and all iterators of this list become invalidated. Since QValueList is highly tuned for performance you wont see warnings if you use invalid iterators, because it is impossible for an iterator to check wether it is valid or not.
Inserts the value x at the end of the list.
Returns an iterator pointing at the inserted item.
See also: insert() and prepend().
Returns an iterator pointing to the item at position i in the list, or end() if the index is out of range.
Returns an iterator pointing to the item at position i in the list, or end() if the index is out of range.
Returns an iterator pointing to the first element in the list. This iterator equals end() if the list is empty;
Returns an iterator pointing to the first element in the list. This iterator equals end() if the list is empty;
Removes all items from the list.
See also: remove().
Counts and returns the number of occurrences of the value x in the list.
Returns the number of items in the list.
See also: isEmpty().
[protected]
If the list does not share its data with another QValueList instance, then nothing happens, otherwise the function creates a new copy of this data and detaches from the shared one. This function is called whenever the list is modified. The implicit sharing mechanism is implemented this way.
Returns an iterator pointing behind the last element in the list. This iterator equals begin() if the list is empty.
Returns an iterator pointing behind the last element in the list. This iterator equals begin() if the list is empty.
Finds the first occurrence of x in the list starting at the position given by it.
Returns end() if no item did match.
Finds the first occurrence of x in the list.
Returns end() if no item did match.
Finds the first occurrence of x in the list starting at the position given by it.
Returns end() if no item did match.
Finds the first occurrence of x in the list.
Returns end() if no item did match.
Returns the first index of the value x in the list or -1 if no such value can be found in the list.
Returns a reference to the first item in the list or the item referenced by end() if no such items exists. Please note that you may not change the value the end() Iterator is pointing to.
Returns a reference to the first item in the list or the item referenced by end() if no such items exists.
Returns an iterator pointing to the last element in the list or end() if no such item exists.
See also: last().
Returns an iterator pointing to the last element in the list or end() if no such item exists.
See also: last().
Inserts the value x in front of the iterator it.
Returns an iterator pointing at the inserted item.
See also: append() and prepend().
Returns TRUE if the list is empty, i.e. count() == 0. Returns FALSE otherwise.
See also: count().
Returns a reference to the last item in the list or the item referenced by end() if no such item exists. Please note that you may not change the value the end() Iterator is pointing to.
See also: end(), first() and fromLast().
Returns a reference to the last item in the list or the item referenced by end() if no such item exists.
See also: end(), first() and fromLast().
Compares both lists.
Returns TRUE if both list are unequal.
Creates a new list and fills it with the elements of this list. Then the elements of l are appended.
Returns the new list.
Adds list to this list.
Returns a reference to this list.
Adds the value x to the end of the list.
Returns a reference to the list.
Adds the value x to the end of the list.
Returns a reference to the list.
Assigns l to this list and returns a reference to this list.
All iterators of the current list become invalidated by this operation. The cost of such an assignment is O(1) since QValueList is implict shared.
Compares both lists.
Returns TRUE if both list are equal.
Returns a reference to the item with index i in the list. It is up to you to check wether this item really exists. You can do that easily with the count() function. However this operator does not check wether i is in range and will deliver undefined results if it does not exist. In contrast to the const operator[] you may manipulate the value returned by this operator.
Returns a const reference to the item with index i in the list. It is up to you to check wether this item really exists. You can do that easily with the count() function. However this operator does not check wether i is in range and will deliver undefined results if it does not exist.
Inserts the value x at the end of the list.
Returns an iterator pointing at the inserted item.
See also: insert() and append().
Removes the item at position it in the list.
Returns an iterator pointing to the item following the removed on or end() if the last item was deleted.
See also: clear().
Removes all items which have the value x.
See also: clear().
Writes a list to the stream. The type T stored in the list must implement the streaming operator, too.
Reads a list from the stream. The type T stored in the list must implement the streaming operator, too.
Search the documentation, FAQ, qt-interest archive and more (uses
www.troll.no):
This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.
Copyright © 1999 Troll Tech | Trademarks | Qt version 2.0.2
|