QSet Class

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

Header: #include <QSet>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

class const_iterator
class iterator
typedef ConstIterator
typedef const_pointer
typedef const_reference
typedef difference_type
typedef key_type
typedef pointer
typedef reference
typedef size_type
typedef value_type

Public Functions

iterator begin()
void clear()
const_iterator constFind(const T &value) const
bool contains(const T &value) const
bool contains(const QSet<T> &set) const
int count() const
void detach()
bool empty() const
iterator find(const T &value)
const_iterator find(const T &value) const
iterator insert(const T &value)
QSet<T> &intersect(const QSet<T> &other)
bool intersects(const QSet<T> &other) const
bool isDetached() const
bool remove(const T &value)
void setSharable(bool sharable)
void squeeze()
QSet<T> &subtract(const QSet<T> &other)
QList<T> toList() const
QSet<T> &unite(const QSet<T> &other)
QList<T> values() const
QSet<T> operator&(const QSet<T> &other) const
QSet<T> &operator&=(const QSet<T> &other)
QSet<T> &operator&=(const T &value)
QSet<T> operator+(const QSet<T> &other) const
QSet<T> &operator+=(const QSet<T> &other)
QSet<T> &operator+=(const T &value)
QSet<T> operator-(const QSet<T> &other) const
QSet<T> &operator-=(const QSet<T> &other)
QSet<T> &operator-=(const T &value)
QSet<T> &operator<<(const T &value)
QSet<T> operator|(const QSet<T> &other) const
QSet<T> &operator|=(const QSet<T> &other)
QSet<T> &operator|=(const T &value)

Static Public Members

QSet<T> fromList(const QList<T> &list)

Detailed Description

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()) {
     QWidget *w = i.next();
     qDebug() << w;
 }

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 (const QString &value, set)
     qDebug() << value;

Items can be removed from the set using remove(). There is 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 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.

Member Type Documentation

typedef QSet::ConstIterator

Qt-style synonym for QSet::const_iterator.

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.

Member Function Documentation

iterator QSet::begin()

void QSet::clear()

const_iterator QSet::constFind(const T &value) const

bool QSet::contains(const T &value) const

bool QSet::contains(const QSet<T> &set) const

int QSet::count() const

void QSet::detach()

bool QSet::empty() const

iterator QSet::find(const T &value)

const_iterator QSet::find(const T &value) const

[static] QSet<T> QSet::fromList(const QList<T> &list)

iterator QSet::insert(const T &value)

QSet<T> &QSet::intersect(const QSet<T> &other)

bool QSet::intersects(const QSet<T> &other) const

bool QSet::isDetached() const

bool QSet::remove(const T &value)

void QSet::setSharable(bool sharable)

void QSet::squeeze()

QSet<T> &QSet::subtract(const QSet<T> &other)

QList<T> QSet::toList() const

QSet<T> &QSet::unite(const QSet<T> &other)

QList<T> QSet::values() const

QSet<T> QSet::operator&(const QSet<T> &other) const

QSet<T> &QSet::operator&=(const QSet<T> &other)

QSet<T> &QSet::operator&=(const T &value)

QSet<T> QSet::operator+(const QSet<T> &other) const

QSet<T> &QSet::operator+=(const QSet<T> &other)

QSet<T> &QSet::operator+=(const T &value)

QSet<T> QSet::operator-(const QSet<T> &other) const

QSet<T> &QSet::operator-=(const QSet<T> &other)

QSet<T> &QSet::operator-=(const T &value)

QSet<T> &QSet::operator<<(const T &value)

QSet<T> QSet::operator|(const QSet<T> &other) const

QSet<T> &QSet::operator|=(const QSet<T> &other)

QSet<T> &QSet::operator|=(const T &value)