QSet Class | Qt Core | Qt 6.11.2
Description: The QSet class is a template class that provides a hash-table-based set.

- Qt 6.11
- Qt Core
- C++ Classes
- QSet
- Qt 6.11.2 Reference Documentation

# QSet Class

template <typename T> class QSet

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

Header: | `#include <QSet>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- QSet is part of Implicitly Shared Classes.

This class is equality-comparable.

Note: All functions in this class are reentrant.

## Public Types

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

## Public Functions

| QSet()
| QSet(std::initializer_list<T> list)
| QSet(InputIterator first, InputIterator last)
QSet<T>::const_iterator | begin() const
QSet<T>::iterator | begin()
qsizetype | capacity() const
QSet<T>::const_iterator | cbegin() const
QSet<T>::const_iterator | cend() const
void | clear()
QSet<T>::const_iterator | constBegin() const
QSet<T>::const_iterator | constEnd() const
QSet<T>::const_iterator | constFind(const T &value) const
bool | contains(const QSet<T> &other) const
bool | contains(const T &value) const
qsizetype | count() const
bool | empty() const
QSet<T>::const_iterator | end() const
QSet<T>::iterator | end()
QSet<T>::iterator | erase(QSet<T>::const_iterator pos)
QSet<T>::const_iterator | find(const T &value) const
QSet<T>::iterator | find(const T &value)
QSet<T>::iterator | insert(const T &value)
`(since 6.1)` QSet<T>::iterator | insert(T &&value)
`(since 6.1)` QSet<T>::iterator | insert(QSet<T>::const_iterator it, const T &value)
QSet<T> & | intersect(const QSet<T> &other)
bool | intersects(const QSet<T> &other) const
bool | isEmpty() const
bool | remove(const T &value)
`(since 6.1)` qsizetype | removeIf(Pred pred)
void | reserve(qsizetype size)
qsizetype | size() const
void | squeeze()
QSet<T> & | subtract(const QSet<T> &other)
void | swap(QSet<T> &other)
QSet<T> & | unite(QSet<T> &&other)
QSet<T> & | unite(const QSet<T> &other)
QList<T> | values() const
QSet<T> & | operator&=(const QSet<T> &other)
QSet<T> & | operator&=(const T &value)
QSet<T> & | operator+=(QSet<T> &&other)
QSet<T> & | operator+=(const QSet<T> &other)
QSet<T> & | operator+=(const T &value)
QSet<T> & | operator-=(const QSet<T> &other)
QSet<T> & | operator-=(const T &value)
QSet<T> & | operator<<(const T &value)
QSet<T> & | operator|=(QSet<T> &&other)
QSet<T> & | operator|=(const QSet<T> &other)
QSet<T> & | operator|=(const T &value)

## Related Non-Members

`(since 6.1)` qsizetype | erase_if(QSet<T> &set, Predicate pred)
bool | operator!=(const QSet<T> &lhs, const QSet<T> &rhs)
QSet<T> | operator&(QSet<T> &&lhs, const QSet<T> &rhs)
QSet<T> | operator&(const QSet<T> &lhs, const QSet<T> &rhs)
QSet<T> | operator+(QSet<T> &&lhs, QSet<T> &&rhs)
QSet<T> | operator+(QSet<T> &&lhs, const QSet<T> &rhs)
QSet<T> | operator+(const QSet<T> &lhs, QSet<T> &&rhs)
QSet<T> | operator+(const QSet<T> &lhs, const QSet<T> &rhs)
QSet<T> | operator-(QSet<T> &&lhs, const QSet<T> &rhs)
QSet<T> | operator-(const QSet<T> &lhs, const QSet<T> &rhs)
QDataStream & | operator<<(QDataStream &out, const QSet<T> &set)
bool | operator==(const QSet<T> &lhs, const QSet<T> &rhs)
QDataStream & | operator>>(QDataStream &in, QSet<T> &set)
QSet<T> | operator|(QSet<T> &&lhs, QSet<T> &&rhs)
QSet<T> | operator|(QSet<T> &&lhs, const QSet<T> &rhs)
QSet<T> | operator|(const QSet<T> &lhs, QSet<T> &&rhs)
QSet<T> | operator|(const QSet<T> &lhs, const QSet<T> &rhs)

## Detailed Description

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:

     for (auto i = set.cbegin(), end = set.cend(); i != end; ++i)
         qDebug() << *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 range-based for:

     QSet<QString> set;
     //...
     for (const auto &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

### QSet::ConstIterator

Qt-style synonym for QSet::const_iterator.

### QSet::Iterator

Qt-style synonym for QSet::iterator.

### QSet::const_pointer

Typedef for const T *. Provided for STL compatibility.

### QSet::const_reference

Typedef for const T &. Provided for STL compatibility.

### QSet::difference_type

Typedef for const ptrdiff_t. Provided for STL compatibility.

### QSet::key_type

Typedef for T. Provided for STL compatibility.

### QSet::pointer

Typedef for T *. Provided for STL compatibility.

### QSet::reference

Typedef for T &. Provided for STL compatibility.

### QSet::size_type

Typedef for int. Provided for STL compatibility.

### QSet::value_type

Typedef for T. Provided for STL compatibility.

## Member Function Documentation

### `[noexcept]` QSet::QSet()

Constructs an empty set.

See also clear().

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

Constructs a set with a copy of each of the elements in the initializer list list.

### template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> QSet::QSet(InputIterator first, InputIterator last)

Constructs a set with the contents in the iterator range [first, last).

The value type of `InputIterator` must be convertible to `T`.

Note: If the range [first, last) contains duplicate elements, the first one is retained.

### `[noexcept]` QSet<T>::const_iterator QSet::begin() const

Returns a const STL-style iterator positioned at the first item in the set.

See also constBegin() and end().

### QSet<T>::iterator QSet::begin()

Returns a non-const STL-style iterator positioned at the first item in the set.

This is an overloaded function.

### qsizetype QSet::capacity() const

Returns the number of buckets in the set'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().

### `[noexcept]` QSet<T>::const_iterator QSet::cbegin() const

Returns a const STL-style iterator positioned at the first item in the set.

See also begin() and cend().

### `[noexcept]` QSet<T>::const_iterator QSet::cend() const

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

See also cbegin() and end().

### void QSet::clear()

Removes all elements from the set.

See also remove().

### `[noexcept]` QSet<T>::const_iterator QSet::constBegin() const

Returns a const STL-style iterator positioned at the first item in the set.

See also begin() and constEnd().

### `[noexcept]` QSet<T>::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().

### QSet<T>::const_iterator QSet::constFind(const T &value) const

Returns a const iterator positioned at the item value in the set. If the set contains no item value, the function returns constEnd().

See also find() and contains().

### bool QSet::contains(const QSet<T> &other) const

Returns `true` if the set contains all items from the other set; otherwise returns `false`.

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

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

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

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

### qsizetype 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().

### `[noexcept]` QSet<T>::const_iterator QSet::end() const

Returns a const STL-style iterator positioned at the imaginary item after the last item in the set.

See also constEnd() and begin().

### QSet<T>::iterator QSet::end()

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

This is an overloaded function.

### QSet<T>::iterator QSet::erase(QSet<T>::const_iterator pos)

Removes the item at the iterator position pos from the set, and returns an iterator positioned at 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.

Note: The iterator pos must be valid and dereferenceable. Calling this method on any other iterator, including its own end(), results in undefined behavior. In particular, even the begin() iterator of an empty set cannot be dereferenced.

See also remove() and find().

### QSet<T>::const_iterator QSet::find(const T &value) const

Returns a const iterator positioned at the item value in the set. If the set contains no item value, the function returns constEnd().

See also constFind() and contains().

### QSet<T>::iterator QSet::find(const T &value)

Returns a non-const iterator positioned at the item value in the set. If the set contains no item value, the function returns end().

This is an overloaded function.

### QSet<T>::iterator QSet::insert(const T &value)

Inserts item value into the set, if value isn't already in the set, and returns an iterator pointing at the inserted item.

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

### `[since 6.1]` QSet<T>::iterator QSet::insert(T &&value)

This is an overloaded function.

This function was introduced in Qt 6.1.

### `[since 6.1]` QSet<T>::iterator QSet::insert(QSet<T>::const_iterator it, const T &value)

Inserts item value into the set, if value isn't already in the set, and returns an iterator pointing at the inserted item.

The iterator it is ignored.

This function is provided for compatibility with the STL.

This is an overloaded function.

This function was introduced in Qt 6.1.

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

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

Removes all items from this set that are not contained in the other set. A reference to this set is returned.

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

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

Returns `true` if this set has at least one item in common with other.

See also contains() and intersect().

### 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().

### `[since 6.1]` template <typename Pred> qsizetype QSet::removeIf(Pred pred)

Removes, from this set, all elements for which the predicate pred returns `true`. Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

### void QSet::reserve(qsizetype size)

Ensures that the set'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. For example:

     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().

### qsizetype QSet::size() const

Returns the number of items in the set.

See also isEmpty() and count().

### void QSet::squeeze()

Reduces the size of the set'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 items from this set that are contained in the other set. Returns a reference to this set.

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

### `[noexcept]` void QSet::swap(QSet<T> &other)

Swaps this set with other. This operation is very fast and never fails.

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

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

Each item in the other set that isn't already in this set is inserted into this set. A reference to this set is returned.

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

### QList<T> QSet::values() const

Returns a new QList containing the elements in the set. The order of the elements in the QList is undefined.

Note: Since Qt 5.14, range constructors are available for Qt's generic container classes and should be used in place of this method.

This function creates a new list, in linear time. The time and memory use that entails can be avoided by iterating from constBegin() to constEnd().

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

Same as intersect(other).

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

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

Same as intersect(other), if we consider other to be a set that contains the singleton value.

This is an overloaded function.

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

Same as subtract(other).

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

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

Removes the occurrence of item value from the set, if it is found, and returns a reference to the set. If the value is not contained the set, nothing is removed.

See also remove().

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

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

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

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

See also insert().

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

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

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

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

Same as unite(other).

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

## Related Non-Members

### `[since 6.1]` template <typename T, typename Predicate> qsizetype erase_if(QSet<T> &set, Predicate pred)

Removes all elements for which the predicate pred returns true from the set set. Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

### `[noexcept]` bool operator!=(const QSet<T> &lhs, const QSet<T> &rhs)

Returns `true` if the lhs set is not equal to the rhs set; otherwise returns `false`.

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

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

See also operator==().

### QSet<T> operator&(const QSet<T> &lhs, const QSet<T> &rhs)

### QSet<T> operator&(QSet<T> &&lhs, const QSet<T> &rhs)

Returns a new QSet that is the intersection of sets lhs and rhs.

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

### QSet<T> operator-(const QSet<T> &lhs, const QSet<T> &rhs)

### QSet<T> operator-(QSet<T> &&lhs, const QSet<T> &rhs)

Returns a new QSet that is the set difference of sets lhs and rhs.

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

### template <typename T> QDataStream &operator<<(QDataStream &out, const QSet<T> &set)

Writes the set to stream out.

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

See also Format of the QDataStream operators.

### `[noexcept]` bool operator==(const QSet<T> &lhs, const QSet<T> &rhs)

Returns `true` if the lhs set is equal to the rhs set; otherwise returns `false`.

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

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

See also operator!=().

### template <typename T> 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.

### QSet<T> operator|(const QSet<T> &lhs, const QSet<T> &rhs)

### QSet<T> operator|(const QSet<T> &lhs, QSet<T> &&rhs)

### QSet<T> operator|(QSet<T> &&lhs, const QSet<T> &rhs)

### QSet<T> operator|(QSet<T> &&lhs, QSet<T> &&rhs)

### QSet<T> operator+(const QSet<T> &lhs, const QSet<T> &rhs)

### QSet<T> operator+(const QSet<T> &lhs, QSet<T> &&rhs)

### QSet<T> operator+(QSet<T> &&lhs, const QSet<T> &rhs)

### QSet<T> operator+(QSet<T> &&lhs, QSet<T> &&rhs)

Returns a new QSet that is the union of sets lhs and rhs.

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