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 Iterator
typedef const_pointer
typedef const_reference
typedef const_reverse_iterator
typedef difference_type
typedef key_type
typedef pointer
typedef reference
typedef reverse_iterator
typedef size_type
typedef value_type

Public Functions

QSet()
QSet(std::initializer_list<T> list)
QSet::iterator begin()
QSet::const_iterator begin() const
int capacity() const
QSet::const_iterator cbegin() const
QSet::const_iterator cend() const
void clear()
QSet::const_iterator constBegin() const
QSet::const_iterator constEnd() const
QSet::const_iterator constFind(const T &value) const
bool contains(const T &value) const
bool contains(const QSet<T> &set) const
int count() const
QSet::const_reverse_iterator crbegin() const
QSet::const_reverse_iterator crend() const
void detach()
bool empty() const
QSet::iterator end()
QSet::const_iterator end() const
QSet::iterator erase(QSet::iterator i)
QSet::iterator erase(QSet::const_iterator i)
QSet::iterator find(const T &value)
QSet::const_iterator find(const T &value) const
QSet::iterator insert(const T &value)
QSet<T> &intersect(const QSet<T> &other)
bool intersects(const QSet<T> &other) const
bool isDetached() const
bool isEmpty() const
QSet::reverse_iterator rbegin()
QSet::const_reverse_iterator rbegin() const
bool remove(const T &value)
QSet::reverse_iterator rend()
QSet::const_reverse_iterator rend() const
void reserve(int size)
void setSharable(bool sharable)
int size() const
void squeeze()
QSet<T> &subtract(const QSet<T> &other)
void swap(QSet<T> &other)
QList<T> toList() const
QSet<T> &unite(const QSet<T> &other)
QList<T> values() const
bool operator!=(const QSet<T> &other) 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)
bool operator==(const QSet<T> &other) const
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())
      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 (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::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::const_reverse_iterator

The QSet::const_reverse_iterator typedef provides an STL-style const reverse iterator for QSet.

It is simply a typedef for std::reverse_iterator<QSet::const_iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QSet::rbegin(), QSet::rend(), QSet::reverse_iterator, and QSet::const_iterator.

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::reverse_iterator

The QSet::reverse_iterator typedef provides an STL-style non-const reverse iterator for QSet.

It is simply a typedef for std::reverse_iterator<QSet::iterator>.

Warning: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See also QSet::rbegin(), QSet::rend(), QSet::const_reverse_iterator, and QSet::iterator.

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

QSet::QSet()

Default constructs an instance of QSet.

QSet::QSet(std::initializer_list<T> list)

Default constructs an instance of QSet.

QSet::iterator QSet::begin()

QSet::const_iterator QSet::begin() const

int QSet::capacity() const

QSet::const_iterator QSet::cbegin() const

QSet::const_iterator QSet::cend() const

void QSet::clear()

QSet::const_iterator QSet::constBegin() const

QSet::const_iterator QSet::constEnd() const

QSet::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

QSet::const_reverse_iterator QSet::crbegin() const

QSet::const_reverse_iterator QSet::crend() const

void QSet::detach()

bool QSet::empty() const

QSet::iterator QSet::end()

QSet::const_iterator QSet::end() const

QSet::iterator QSet::erase(QSet::iterator i)

QSet::iterator QSet::erase(QSet::const_iterator i)

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

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

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

QSet::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::isEmpty() const

QSet::reverse_iterator QSet::rbegin()

QSet::const_reverse_iterator QSet::rbegin() const

bool QSet::remove(const T &value)

QSet::reverse_iterator QSet::rend()

QSet::const_reverse_iterator QSet::rend() const

void QSet::reserve(int size)

void QSet::setSharable(bool sharable)

int QSet::size() const

void QSet::squeeze()

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

void QSet::swap(QSet<T> &other)

QList<T> QSet::toList() const

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

QList<T> QSet::values() const

bool QSet::operator!=(const QSet<T> &other) 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)

bool QSet::operator==(const QSet<T> &other) 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)