Qt:Документация 4.3.2/qset

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

Содержание

[править] QSet Class Reference
[модуль QtCore ]

The QSet class is a template class that provides a hash-table-based set. Далее...

 #include <QSet>

Примечание: все функции в этом классе реентерабельны.

[править] Открытые типы

[править] Открытые функции

[править] Статические открытые члены

[править] Связанные не-члены

  • QDataStream & operator<< ( QDataStream & out, const QSet<T> & set )
  • QDataStream & operator>> ( QDataStream & in, QSet<T> & set )

[править] Подробное описание

The QSet class is a template class that provides a hash-table-based set.

QSet<T> is one of Qt's generic container classes. It stores values in an unspecified order and provides very fast lookup of the values. Internally, QSet<T> is implemented as a QHash.

Here's an example QSet with QString values:

 QSet<QString> set;

To insert a value into the set, use insert():

 set.insert("one");
 set.insert("three");
 set.insert("seven");

Another way to insert items into the set is to use operator<<():

 set << "twelve" << "fifteen" << "nineteen";

To test whether an item belongs to the set or not, use contains():

 if (!set.contains("ninety-nine"))
     ...

If you want to navigate through all the values stored in a QSet, you can use an iterator. QSet supports both Java-style iterators ( QSetIterator and QMutableSetIterator) and STL-style iterators ( QSet::iterator and QSet::const_iterator). Here's how to iterate over a QSet< QWidget *> using a Java-style iterator:

 QSetIterator<QWidget *> i(set);
 while (i.hasNext())
     qDebug() << i.next();

Here's the same code, but using an STL-style iterator:

 QSet<QWidget *>::const_iterator i = set.constBegin();
 while (i != set.constEnd()) {
     qDebug() << *i;
     ++i;
 }

QSet is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.

To navigate through a QSet, you can also use foreach:

 QSet<QString> set;
 ...
 foreach (QString value, set)
     qDebug() << value;

Items can be removed from the set using remove(). There's also a clear() function that removes all items.

QSet's value data type must be an assignable data type. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, the type must provide operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key's type. See the QHash documentation for a list of types supported by qHash().

Internally, QSet uses a hash table to perform lookups. The hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many elements the QSet will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

See also QSetIterator, QMutableSetIterator, QHash, and QMap.


[править] Описание типов

[править]
typedef QSet::ConstIterator

Qt-style synonym for QSet::const_iterator.

[править]
typedef QSet::Iterator

Qt-style synonym for QSet::iterator.

This typedef was introduced in Qt 4.2.

[править]
typedef QSet::const_pointer

Typedef for const T *. Provided for STL compatibility.

[править]
typedef QSet::const_reference

Typedef for const T &. Provided for STL compatibility.

[править]
typedef QSet::difference_type

Typedef for const ptrdiff_t. Provided for STL compatibility.

[править]
typedef QSet::key_type

Typedef for T. Provided for STL compatibility.

[править]
typedef QSet::pointer

Typedef for T *. Provided for STL compatibility.

[править]
typedef QSet::reference

Typedef for T &. Provided for STL compatibility.

[править]
typedef QSet::size_type

Typedef for int. Provided for STL compatibility.

[править]
typedef QSet::value_type

Typedef for T. Provided for STL compatibility.


[править] Описание функций-членов

[править]
QSet::QSet ()

Constructs an empty set.

See also clear().

[править]
QSet::QSet ( const QSet<T> & other )

Создаёт копию other.

This operation occurs in constant time, because QSet is implicitly shared. This makes returning a QSet from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes linear time.

See also operator=().

[править]
const_iterator QSet::begin () const

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

See also constBegin() and end().

[править]
iterator QSet::begin ()

Эта перегруженная функция предоставлена для удобства.

Returns a non-const STL-style iterator pointing to the first item in the set.

Эта функция была введена в Qt 4.2.

[править]
int QSet::capacity () const

Returns the number of buckets in the QSet's internal hash table.

The sole purpose of this function is to provide a means of fine tuning QSet's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the set, call size().

See also reserve() and squeeze().

[править]
void QSet::clear ()

Removes all elements from the set.

See also remove().

[править]
const_iterator QSet::constBegin () const

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

See also begin() and constEnd().

[править]
const_iterator QSet::constEnd () const

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

See also constBegin() and end().

[править]
const_iterator QSet::constFind ( const T & value ) const

Returns a const iterator pointing to the item value in the set.

If the set contains no item value, the function returns constEnd().

Эта функция была введена в Qt 4.2.

See also find() and contains().

[править]
bool QSet::contains ( const T & value ) const

Returns true if the set contains item value; otherwise returns false.

See also insert(), remove(), and find().

[править]
int QSet::count () const

Same as size().

[править]
bool QSet::empty () const

Returns true if the set is empty. This function is provided for STL compatibility. It is equivalent to isEmpty().

[править]
const_iterator QSet::end () const

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

See also constEnd() and begin().

[править]
iterator QSet::end ()

Эта перегруженная функция предоставлена для удобства.

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

Эта функция была введена в Qt 4.2.

[править]
iterator QSet::erase ( iterator pos )

Removes the item associated with the iterator pos from the set, and returns an iterator to the next item in the set.

Unlike remove(), this function never causes QSet to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the set.

Эта функция была введена в Qt 4.2.

See also remove() and find().

[править]
const_iterator QSet::find ( const T & value ) const

Returns a const iterator pointing to the item value in the set.

If the set contains no item value, the function returns constEnd().

Эта функция была введена в Qt 4.2.

See also constFind() and contains().

[править]
iterator QSet::find ( const T & value )

Эта перегруженная функция предоставлена для удобства.

Returns a non-const iterator pointing to the item value in the set.

If the set contains no item value, the function returns end().

Эта функция была введена в Qt 4.2.

[править]
QSet<T> QSet::fromList ( const QList<T> & list ) [static]

Returns a QSet object with the data contained in list. Since QSet doesn't allow duplicates, the resulting QSet might be smaller than the original list was.

Пример:

 QStringList list;
 list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia";
 
 QSet<QString> set = QSet<QString>::fromList(list);
 set.contains("Julia");  // returns true
 set.contains("Mike");   // returns true
 set.size();             // returns 2

See also toList() and QList::toSet().

[править]
const_iterator QSet::insert ( const T & value )

Inserts a new item value. If value already exists in the set, nothing happens.

For historical reasons, the return type is a non-const iterator. This might change in a future version.

See also operator<<(), remove(), and contains().

[править]
QSet<T> & QSet::intersect ( const QSet<T> & other )

Removes any items in this set that don't exist in the other set.

See also operator&=(), unite(), and subtract().

[править]
bool QSet::isEmpty () const

Returns true if the set contains no elements; otherwise returns false.

See also size().

[править]
bool QSet::remove ( const T & value )

Removes any occurrence of item value from the set. Returns true if an item was actually removed; otherwise returns false.

See also contains() and insert().

[править]
void QSet::reserve ( int size )

Ensures that the QSet's internal hash table consists of at least size buckets.

This function is useful for code that needs to build a huge set and wants to avoid repeated reallocation. Пример:

 QSet<QString> set;
 set.reserve(20000);
 for (int i = 0; i < 20000; ++i)
     set.insert(values[i]);

Ideally, size should be slightly more than the maximum number of elements expected in the set. size doesn't have to be prime, because QSet will use a prime number internally anyway. If size is an underestimate, the worst that will happen is that the QSet will be a bit slower.

In general, you will rarely ever need to call this function. QSet's internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.

See also squeeze() and capacity().

[править]
int QSet::size () const

Returns the number of items in the set.

See also isEmpty() and count().

[править]
void QSet::squeeze ()

Reduces the size of the QSet's internal hash table to save memory.

The sole purpose of this function is to provide a means of fine tuning QSet's memory usage. In general, you will rarely ever need to call this function.

See also reserve() and capacity().

[править]
QSet<T> & QSet::subtract ( const QSet<T> & other )

Removes all the items in the other set from this set.

See also operator-=(), unite(), and intersect().

[править]
QList<T> QSet::toList () const

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

Пример:

 QSet<QString> set;
 set << "red" << "green" << "blue" << ... << "black";
 
 QList<QString> list = set.toList();
 qSort(list);

See also fromList(), QList::fromSet(), and qSort().

[править]
QSet<T> & QSet::unite ( const QSet<T> & other )

Inserts all the items in the other set into this set.

See also operator|=(), intersect(), and subtract().

[править]
QList<T> QSet::values () const

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

This is the same as toList().

See also fromList(), QList::fromSet(), and qSort().

[править]
bool QSet::operator!= ( const QSet<T> & other ) const

Returns true if other is not equal to this set; otherwise returns false.

Two sets are considered equal if they contain the same elements.

This function requires the value type to implement operator==().

Смотрите также operator==().

[править]
QSet<T> QSet::operator& ( const QSet<T> & other ) const

Returns the intersection of this set and other.

See also intersect(), operator&=(), operator|(), and operator-().

[править]
QSet<T> & QSet::operator&= ( const QSet<T> & other )

Syntactic sugar for intersect(other).

See also operator&(), operator|=(), and operator-=().

[править]
QSet<T> & QSet::operator&= ( const T & value )

Эта перегруженная функция предоставлена для удобства.

Syntactic sugar for intersect(other), with other being a set that only contains value.

[править]
QSet<T> QSet::operator+ ( const QSet<T> & other ) const

Returns the union of this set and other.

See also unite(), operator|=(), operator&(), and operator-().

[править]
QSet<T> & QSet::operator+= ( const QSet<T> & other )

Syntactic sugar for unite(other).

See also operator|(), operator&=(), and operator-=().

[править]
QSet<T> & QSet::operator+= ( const T & value )

Эта перегруженная функция предоставлена для удобства.

Inserts a new item value and returns a reference to this set. If value already exists in the set, the set is left unchanged.

See also insert().

[править]
QSet<T> QSet::operator- ( const QSet<T> & other ) const

Returns the set difference of this set with other.

See also subtract(), operator-=(), operator|(), and operator&().

[править]
QSet<T> & QSet::operator-= ( const QSet<T> & other )

Syntactic sugar for subtract(other).

See also operator-(), operator|=(), and operator&=().

[править]
QSet<T> & QSet::operator-= ( const T & value )

Эта перегруженная функция предоставлена для удобства.

Removes any occurrence of item value from the set and returns a reference to this set. If value didn't exist in the set, the set is left unchanged.

See also remove().

[править]
QSet<T> & QSet::operator<< ( const T & value )

Inserts a new item value and returns a reference to this set. If value already exists in the set, the set is left unchanged.

See also insert().

[править]
QSet<T> & QSet::operator= ( const QSet<T> & other )

Assigns other to this set and returns a reference to this set.

[править]
bool QSet::operator== ( const QSet<T> & other ) const

Returns true if other is equal to this set; otherwise returns false.

Two sets are considered equal if they contain the same elements.

This function requires the value type to implement operator==().

Смотирте также operator!=().

[править]
QSet<T> QSet::operator| ( const QSet<T> & other ) const

Returns the union of this set and other.

See also unite(), operator|=(), operator&(), and operator-().

[править]
QSet<T> & QSet::operator|= ( const QSet<T> & other )

Syntactic sugar for unite(other).

See also operator|(), operator&=(), and operator-=().

[править]
QSet<T> & QSet::operator|= ( const T & value )

Эта перегруженная функция предоставлена для удобства.

Inserts a new item value and returns a reference to this set. If value already exists in the set, the set is left unchanged.

See also insert().


[править] Связанные не-члены

[править]
QDataStream & operator<< ( QDataStream & out, const QSet<T> & set )

Эта перегруженная функция предоставлена для удобства.

Writes the set set to stream out.

This function requires the value type to implement operator<<().

See also Format of the QDataStream operators.

[править]
QDataStream & operator>> ( QDataStream & in, QSet<T> & set )

Эта перегруженная функция предоставлена для удобства.

Reads a set from stream in into set.

This function requires the value type to implement operator>>().

See also Format of the QDataStream operators.



Copyright © 2007 Trolltech Trademarks
Qt 4.3.2