You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
368 lines
12 KiB
368 lines
12 KiB
13 years ago
|
/****************************************************************************
|
||
|
**
|
||
10 years ago
|
** TQt Template Library classes documentation
|
||
13 years ago
|
**
|
||
|
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
|
||
|
**
|
||
10 years ago
|
** This file is part of the TQt GUI Toolkit.
|
||
13 years ago
|
**
|
||
|
** This file may be used under the terms of the GNU General
|
||
|
** Public License versions 2.0 or 3.0 as published by the Free
|
||
|
** Software Foundation and appearing in the files LICENSE.GPL2
|
||
|
** and LICENSE.GPL3 included in the packaging of this file.
|
||
|
** Alternatively you may (at your option) use any later version
|
||
|
** of the GNU General Public License if such license has been
|
||
|
** publicly approved by Trolltech ASA (or its successors, if any)
|
||
10 years ago
|
** and the KDE Free TQt Foundation.
|
||
13 years ago
|
**
|
||
|
** Please review the following information to ensure GNU General
|
||
13 years ago
|
** Public Licensing requirements will be met:
|
||
13 years ago
|
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
|
||
|
** If you are unsure which license is appropriate for your use, please
|
||
|
** review the following information:
|
||
|
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||
|
** or contact the sales department at sales@trolltech.com.
|
||
|
**
|
||
|
** This file may be used under the terms of the Q Public License as
|
||
|
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
|
||
|
** included in the packaging of this file. Licensees holding valid Qt
|
||
|
** Commercial licenses may use this file in accordance with the Qt
|
||
|
** Commercial License Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
|
||
|
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
|
||
|
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
|
||
|
** herein.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
|
/*!
|
||
|
|
||
|
\page qt-template-lib.html
|
||
|
|
||
10 years ago
|
\title TQt Template Library
|
||
13 years ago
|
|
||
3 months ago
|
The TQt Template Library (TQTL) is a set of templates that provide
|
||
13 years ago
|
object containers. If a suitable STL implementation is not available
|
||
3 months ago
|
on all your target platforms, the TQTL can be used instead. It provides
|
||
13 years ago
|
a list of objects, a vector (dynamic array) of objects, a map relating
|
||
|
one type to another (also called a dictionary or associative array),
|
||
|
and associated \link #Iterators iterators\endlink and \link
|
||
|
#Algorithms algorithms\endlink. A container is an object which
|
||
|
contains and manages other objects and provides iterators that allow
|
||
|
the contained objects to be accessed.
|
||
|
|
||
3 months ago
|
The TQTL classes' naming conventions are consistent with the other Qt
|
||
13 years ago
|
classes (e.g., count(), isEmpty()). They also provide extra functions
|
||
|
for compatibility with STL algorithms, such as size() and empty().
|
||
|
Programmers already familiar with the STL \c map can use the
|
||
|
STL-compatible functions if preferred.
|
||
|
|
||
3 months ago
|
Compared to the STL, the TQTL only contains the most important features
|
||
|
of the STL container API. Compared with the STL, TQTL has no platform
|
||
13 years ago
|
differences, but is often a little slower and often expands to less
|
||
|
object code.
|
||
|
|
||
|
If you cannot make copies of the objects you want to store you should
|
||
6 months ago
|
use TQPtrCollection and friends, all of which operate on pointers
|
||
13 years ago
|
rather than values. This applies, for example, to all classes derived
|
||
1 year ago
|
from \l TQObject. A TQObject does not have a copy constructor, so using
|
||
13 years ago
|
it as value is impossible. You may choose to store pointers to
|
||
6 months ago
|
TQObjects in a TQValueList, but using TQPtrList directly seems to be the
|
||
|
better choice for this kind of application domain. TQPtrList, like all
|
||
|
other TQPtrCollection based containers, provides far more sanity
|
||
13 years ago
|
checking than a speed-optimized value based container.
|
||
|
|
||
|
If you have objects that implement value semantics, and the STL is not
|
||
10 years ago
|
available on your target platform, the TQt Template Library can be used
|
||
13 years ago
|
instead. Value semantics require at least:
|
||
13 years ago
|
\list
|
||
|
\i a copy constructor;
|
||
|
\i an assignment operator;
|
||
|
\i a defaultconstructor, i.e. a constructor that does not take any arguments.
|
||
|
\endlist
|
||
|
|
||
|
Note that a fast copy constructor is absolutely crucial to achieve
|
||
|
good overall performance of the container, since many copy operations
|
||
|
will occur.
|
||
|
|
||
|
If you intend sorting your data you must implement \c{operator<()} for
|
||
|
your data's class.
|
||
|
|
||
4 months ago
|
Good candidates for value based classes are TQRect, TQPoint, TQSize,
|
||
1 year ago
|
TQString and all simple C++ types, such as int, bool or double.
|
||
13 years ago
|
|
||
10 years ago
|
The TQt Template Library is designed for speed. Iterators are extremely
|
||
13 years ago
|
fast. To achieve this performance, less error checking is done than in
|
||
3 months ago
|
the TQPtrCollection based containers. A TQTL container, for example,
|
||
13 years ago
|
does not track any associated iterators. This makes certain validity
|
||
|
checks, for example when removing items, impossible to perform
|
||
|
automatically, but does lead to extremely good performance.
|
||
|
|
||
|
\target Iterators
|
||
|
\section1 Iterators
|
||
|
|
||
10 years ago
|
The TQt Template Library deals with value objects, not with pointers.
|
||
13 years ago
|
For that reason, there is no other way of iterating over containers
|
||
|
other than with iterators. This is no disadvantage as the size of an
|
||
|
iterator matches the size of a normal pointer.
|
||
|
|
||
|
To iterate over a container, use a loop like this:
|
||
|
\code
|
||
6 months ago
|
typedef TQValueList<int> List;
|
||
13 years ago
|
List list;
|
||
|
for( List::Iterator it = list.begin(); it != list.end(); ++it )
|
||
|
printf( "Number is %i\n", *it );
|
||
|
\endcode
|
||
|
|
||
|
begin() returns the iterator pointing at the first element, while
|
||
|
end() returns an iterator that points \e after the last element. end()
|
||
|
marks an invalid position, so it can never be dereferenced. It's the
|
||
|
break condition in any iteration, whether the start point is from
|
||
|
begin() or fromLast(). For maximum speed, use increment or decrement
|
||
|
iterators with the prefix operator (++it, --it) instead of the postfix
|
||
|
operator (it++, it--), since the former is slightly faster.
|
||
|
|
||
|
The same concept applies to the other container classes:
|
||
|
\code
|
||
6 months ago
|
typedef TQMap<TQString,TQString> Map;
|
||
13 years ago
|
Map map;
|
||
|
for( Map::iterator it = map.begin(); it != map.end(); ++it )
|
||
|
printf( "Key=%s Data=%s\n", it.key().ascii(), it.data().ascii() );
|
||
|
|
||
6 months ago
|
typedef TQValueVector<int> Vector;
|
||
13 years ago
|
Vector vec;
|
||
|
for( Vector::iterator it = vec.begin(); it != vec.end(); ++it )
|
||
|
printf( "Data=%d\n", *it );
|
||
|
\endcode
|
||
|
|
||
|
There are two kind of iterators, the volatile iterator shown in the
|
||
|
examples above and a version that returns a const reference to its
|
||
13 years ago
|
current object, the ConstIterator. Const iterators are required
|
||
13 years ago
|
whenever the container itself is const, such as a member variable
|
||
|
inside a const function. Assigning a ConstIterator to a normal
|
||
|
Iterator is not allowed as it would violate const semantics.
|
||
|
|
||
|
\target Algorithms
|
||
|
\section1 Algorithms
|
||
|
|
||
10 years ago
|
The TQt Template Library defines a number of algorithms that operate on
|
||
13 years ago
|
its containers. These algorithms are implemented as template functions
|
||
|
and provide useful generic code which can be applied to any container
|
||
|
that provides iterators (including your own containers).
|
||
|
|
||
|
\section2 qHeapSort()
|
||
|
|
||
|
qHeapSort() provides a well known sorting algorithm. You can use it
|
||
|
like this:
|
||
|
\code
|
||
6 months ago
|
typedef TQValueList<int> List;
|
||
13 years ago
|
List list;
|
||
|
list << 42 << 100 << 1234 << 12 << 8;
|
||
|
qHeapSort( list );
|
||
|
|
||
|
List list2;
|
||
|
list2 << 42 << 100 << 1234 << 12 << 8;
|
||
|
List::Iterator b = list2.find( 100 );
|
||
|
List::Iterator e = list2.find( 8 );
|
||
|
qHeapSort( b, e );
|
||
|
|
||
|
double arr[] = { 3.2, 5.6, 8.9 };
|
||
|
qHeapSort( arr, arr + 3 );
|
||
|
\endcode
|
||
|
|
||
|
The first example sorts the entire list. The second example sorts only
|
||
|
those elements that fall between the two iterators, i.e. 100, 1234 and
|
||
|
12. The third example shows that iterators act like pointers and can
|
||
|
be treated as such.
|
||
|
|
||
|
If using your own data types you must implement \c{operator<()} for
|
||
|
your data's class.
|
||
|
|
||
|
Naturally, the sorting templates won't work with const iterators.
|
||
|
|
||
13 years ago
|
\target tqSwap
|
||
|
\section2 tqSwap()
|
||
13 years ago
|
|
||
13 years ago
|
tqSwap() exchanges the values of two variables:
|
||
13 years ago
|
\code
|
||
1 year ago
|
TQString second( "Einstein" );
|
||
|
TQString name( "Albert" );
|
||
13 years ago
|
tqSwap( second, name );
|
||
13 years ago
|
\endcode
|
||
|
|
||
13 years ago
|
\target tqCount
|
||
|
\section2 tqCount()
|
||
13 years ago
|
|
||
13 years ago
|
The tqCount() template function counts the number of occurrences of a
|
||
13 years ago
|
value within a container. For example:
|
||
|
\code
|
||
6 months ago
|
TQValueList<int> list;
|
||
13 years ago
|
list.push_back( 1 );
|
||
|
list.push_back( 1 );
|
||
|
list.push_back( 1 );
|
||
|
list.push_back( 2 );
|
||
|
int c = 0;
|
||
13 years ago
|
tqCount( list.begin(), list.end(), 1, c ); // c == 3
|
||
13 years ago
|
\endcode
|
||
|
|
||
13 years ago
|
\target tqFind
|
||
|
\section2 tqFind()
|
||
13 years ago
|
|
||
13 years ago
|
The tqFind() template function finds the first occurrence of a value
|
||
13 years ago
|
within a container. For example:
|
||
|
\code
|
||
6 months ago
|
TQValueList<int> list;
|
||
13 years ago
|
list.push_back( 1 );
|
||
|
list.push_back( 1 );
|
||
|
list.push_back( 1 );
|
||
|
list.push_back( 2 );
|
||
6 months ago
|
TQValueListIterator<int> it = tqFind( list.begin(), list.end(), 2 );
|
||
13 years ago
|
\endcode
|
||
|
|
||
13 years ago
|
\target tqFill
|
||
|
\section2 tqFill()
|
||
13 years ago
|
|
||
13 years ago
|
The tqFill() template function fills a range with copies of a value.
|
||
13 years ago
|
For example:
|
||
|
\code
|
||
6 months ago
|
TQValueVector<int> vec(3);
|
||
13 years ago
|
tqFill( vec.begin(), vec.end(), 99 ); // vec contains 99, 99, 99
|
||
13 years ago
|
\endcode
|
||
|
|
||
13 years ago
|
\target tqEqual
|
||
|
\section2 tqEqual()
|
||
13 years ago
|
|
||
13 years ago
|
The tqEqual() template function compares two ranges for equality of
|
||
13 years ago
|
their elements. Note that the number of elements in each range is not
|
||
|
considered, only if the elements in the first range are equal to the
|
||
|
corresponding elements in the second range (consequently, both ranges
|
||
|
must be valid). For example:
|
||
|
\code
|
||
6 months ago
|
TQValueVector<int> v1(3);
|
||
13 years ago
|
v1[0] = 1;
|
||
|
v1[2] = 2;
|
||
|
v1[3] = 3;
|
||
|
|
||
6 months ago
|
TQValueVector<int> v2(5);
|
||
13 years ago
|
v2[0] = 1;
|
||
|
v2[2] = 2;
|
||
|
v2[3] = 3;
|
||
|
v2[4] = 4;
|
||
|
v2[5] = 5;
|
||
|
|
||
13 years ago
|
bool b = tqEqual( v1.begin(), v2.end(), v2.begin() );
|
||
13 years ago
|
// b == TRUE
|
||
|
\endcode
|
||
|
|
||
13 years ago
|
\target tqCopy
|
||
|
\section2 tqCopy()
|
||
13 years ago
|
|
||
13 years ago
|
The tqCopy() template function copies a range of elements to an
|
||
6 months ago
|
OutputIterator, in this case a TQTextOStreamIterator:
|
||
13 years ago
|
\code
|
||
6 months ago
|
TQValueList<int> list;
|
||
13 years ago
|
list.push_back( 100 );
|
||
|
list.push_back( 200 );
|
||
|
list.push_back( 300 );
|
||
6 months ago
|
TQTextOStream str( stdout );
|
||
|
tqCopy( list.begin(), list.end(), TQTextOStreamIterator(str) );
|
||
13 years ago
|
\endcode
|
||
|
|
||
|
\omit
|
||
|
Here is another example which copies a range of elements from one
|
||
|
container into another. It uses the qBackInserter() template function
|
||
|
which creates a QBackInsertIterator<> whose job is to insert elements
|
||
|
into the end of a container. For example:
|
||
|
|
||
|
\code
|
||
6 months ago
|
TQValueList<int> l;
|
||
13 years ago
|
l.push_back( 100 );
|
||
|
l.push_back( 200 );
|
||
|
l.push_back( 300 );
|
||
6 months ago
|
TQValueVector<int> v;
|
||
13 years ago
|
tqCopy( l.begin(), l.end(), qBackInserter(v) );
|
||
13 years ago
|
\endcode
|
||
|
\endomit
|
||
|
|
||
13 years ago
|
\target tqCopyBackward
|
||
|
\section2 tqCopyBackward()
|
||
13 years ago
|
|
||
13 years ago
|
The tqCopyBackward() template function copies a container or a slice of
|
||
13 years ago
|
a container to an OutputIterator, but in reverse order, for example:
|
||
|
\code
|
||
6 months ago
|
TQValueVector<int> vec(3);
|
||
13 years ago
|
vec.push_back( 100 );
|
||
|
vec.push_back( 200 );
|
||
|
vec.push_back( 300 );
|
||
6 months ago
|
TQValueVector<int> another;
|
||
13 years ago
|
tqCopyBackward( vec.begin(), vec.end(), another.begin() );
|
||
13 years ago
|
// 'another' now contains 100, 200, 300
|
||
|
// however the elements are copied one at a time
|
||
|
// in reverse order (300, 200, then 100)
|
||
|
\endcode
|
||
|
|
||
3 months ago
|
\section2 TQTL Iterators
|
||
13 years ago
|
|
||
10 years ago
|
You can use any TQt Template Library iterator as the OutputIterator.
|
||
13 years ago
|
Just make sure that the right hand of the iterator has as many
|
||
|
elements present as you want to insert. The following example
|
||
|
illustrates this:
|
||
|
|
||
|
\code
|
||
6 months ago
|
TQStringList list1, list2;
|
||
13 years ago
|
list1 << "Weis" << "Ettrich" << "Arnt" << "Sue";
|
||
|
list2 << "Torben" << "Matthias";
|
||
13 years ago
|
tqCopy( list2.begin(), list2.end(), list1.begin() );
|
||
13 years ago
|
|
||
6 months ago
|
TQValueVector<TQString> vec( list1.size(), "Dave" );
|
||
13 years ago
|
tqCopy( list2.begin(), list2.end(), vec.begin() );
|
||
13 years ago
|
\endcode
|
||
|
|
||
|
At the end of this code fragment, the list list1 contains "Torben",
|
||
|
"Matthias", "Arnt" and "Sue", with the prior contents being
|
||
|
overwritten. The vector vec contains "Torben", "Matthias", "Dave" and
|
||
|
"Dave", also with the prior contents being overwritten.
|
||
|
|
||
|
If you write new algorithms, consider writing them as template
|
||
|
functions in order to make them usable with as many containers
|
||
|
as possible. In the above example, you could just as easily print out
|
||
13 years ago
|
a standard C++ array with tqCopy():
|
||
13 years ago
|
|
||
|
\code
|
||
|
int arr[] = { 100, 200, 300 };
|
||
6 months ago
|
TQTextOStream str( stdout );
|
||
|
tqCopy( arr, arr + 3, TQTextOStreamIterator( str ) );
|
||
13 years ago
|
\endcode
|
||
|
|
||
|
\section1 Streaming
|
||
|
|
||
|
All the containers we've mentioned can be serialized with the
|
||
|
appropriate streaming operators. Here is an example.
|
||
|
|
||
|
\code
|
||
5 months ago
|
TQDataStream str(...);
|
||
4 months ago
|
TQValueList<TQRect> list;
|
||
13 years ago
|
// ... fill the list here
|
||
|
str << list;
|
||
|
\endcode
|
||
|
|
||
|
The container can be read in again with:
|
||
|
|
||
|
\code
|
||
4 months ago
|
TQValueList<TQRect> list;
|
||
13 years ago
|
str >> list;
|
||
|
\endcode
|
||
|
|
||
6 months ago
|
The same applies to TQStringList, TQValueStack and TQMap.
|
||
13 years ago
|
*/
|
||
|
|
||
|
/*!
|
||
1 year ago
|
\fn TQPair qMakePair(T1 t1, T2 t2)
|
||
13 years ago
|
|
||
1 year ago
|
\relates TQPair
|
||
13 years ago
|
|
||
|
This is a template convenience function. It is used to create a
|
||
1 year ago
|
TQPair\<\> object that contains \a t1 and \a t2.
|
||
13 years ago
|
*/
|