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 | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| typedef | Container |
| typedef | iterator |
| typedef | const_iterator |
| typedef | value_type |
Public Functions
| QSet() | |
| QSet(std::initializer_list<T> l) | |
| QSet(It first, It last) | |
| qsizetype | size() const |
| qsizetype | count() const |
| bool | isEmpty() const |
| bool | empty() const |
| void | clear() |
| void | reserve(qsizetype n) |
| qsizetype | capacity() const |
| void | squeeze() |
| const_iterator | constFind(const T &value) const |
| bool | isSubsetOf(const QSet &other) const |
| bool | contains(const T &t) const |
| bool | contains(const QSet &other) const |
| iterator | insert(const T &t) |
| bool | remove(const T &t) |
| qsizetype | removeIf(Pred p) |
| iterator | erase(const_iterator it) |
| iterator | find(const T &t) const |
| QSet & | unite(const QSet &o) |
| QSet & | intersect(const QSet &o) |
| QSet & | subtract(const QSet &o) |
| bool | intersects(const QSet &o) const |
| QList<T> | values() const |
| QList<T> | toList() const |
| const_iterator | begin() const |
| const_iterator | end() const |
| const_iterator | cbegin() const |
| const_iterator | cend() const |
| const_iterator | constBegin() const |
| const_iterator | constEnd() const |
| QSet & | operator<<(const T &t) |
| QSet & | operator+=(const T &t) |
| QSet & | operator|=(const QSet &o) |
| QSet & | operator+=(const QSet &o) |
| QSet & | operator&=(const QSet &o) |
| QSet & | operator-=(const QSet &o) |
Detailed Description
QSet<T> stores unique values and tests for membership in constant time on average. The values are kept in no particular order. The value type needs operator== and either a qHash() overload or a std::hash specialization.
QSet<QString> names;
names << "Alice" << "Bob";
names.insert("Alice"); // already present, no change
bool hasBob = names.contains("Bob");
See also QHash and QSetIterator.
Differences from Qt
QSet is implemented on top of std::unordered_set and has no implicit sharing. Its iterators are constant. The predicate passed to removeIf() is called with an iterator rather than with the value. toList() is provided although it was removed from Qt 6.
Member Type Documentation
typedef QSet::const_iterator
The constant iterator type.
typedef QSet::Container
The underlying std::unordered_set type used for storage.
typedef QSet::iterator
The iterator type. It is constant, so the values of a set cannot be modified through it.
typedef QSet::value_type
The type of the values stored in the set, T.
Member Function Documentation
QSet::QSet()
Constructs an empty set.
QSet::QSet(std::initializer_list<T> l)
This is an overloaded function.
Constructs a set with the values in the initializer list l.
template <typename It, typename = typename std::iterator_traits<It>::iterator_category>QSet::QSet(It first, It last)
This is an overloaded function.
Constructs a set with the values in the range from first up to, but not including, last.
const_iterator QSet::begin() const
Returns a constant iterator to the first value in the set.
See also end() and constBegin().
qsizetype QSet::capacity() const
Returns the number of buckets in the hash table of the set.
See also reserve() and squeeze().
const_iterator QSet::cbegin() const
Returns a constant iterator to the first value in the set; the same as begin().
const_iterator QSet::cend() const
Returns a constant iterator to the position after the last value in the set; the same as end().
void QSet::clear()
Removes all values from the set.
See also remove().
const_iterator QSet::constBegin() const
Returns a constant iterator to the first value in the set; the same as begin().
const_iterator QSet::constEnd() const
Returns a constant iterator to the position after the last value in the set; the same as end().
const_iterator QSet::constFind(const T &value) const
Returns an iterator to the value value, or constEnd() if the set does not contain it.
See also find() and contains().
bool QSet::contains(const T &t) const
Returns true if the set contains the value t; otherwise returns false.
bool QSet::contains(const QSet &other) const
This is an overloaded function.
Returns true if the set contains every value of the set other; otherwise returns false.
qsizetype QSet::count() const
Returns the number of values in the set; the same as size().
bool QSet::empty() const
Returns true if the set has no values. Provided for compatibility with the standard library.
See also isEmpty().
const_iterator QSet::end() const
Returns a constant iterator to the position after the last value in the set.
See also begin() and constEnd().
iterator QSet::erase(const_iterator it)
Removes the value that it points to and returns an iterator to the value that followed it.
See also remove().
iterator QSet::find(const T &t) const
Returns an iterator to the value t, or end() if the set does not contain it.
See also contains().
iterator QSet::insert(const T &t)
Inserts the value t into the set if it is not already present, and returns an iterator to the value in the set.
See also remove() and contains().
QSet & QSet::intersect(const QSet &o)
Removes all values from this set that are not in the set o, and returns a reference to this set.
See also unite(), subtract() and intersects().
bool QSet::intersects(const QSet &o) const
Returns true if this set and the set o have at least one value in common; otherwise returns false.
See also intersect().
bool QSet::isEmpty() const
Returns true if the set contains no values; otherwise returns false.
See also size().
bool QSet::isSubsetOf(const QSet &other) const
Returns true if every value of this set is also in other.
This function was introduced in 5.12.
See also contains() and intersects().
QSet & QSet::operator&=(const QSet &o)
Removes all values from this set that are not in the set o, and returns a reference to this set; the same as intersect().
QSet & QSet::operator+=(const T &t)
This is an overloaded function.
Inserts the value t into the set and returns a reference to this set.
QSet & QSet::operator+=(const QSet &o)
This is an overloaded function.
Inserts all values of the set o into this set; the same as unite().
QSet & QSet::operator-=(const QSet &o)
Removes all values of the set o from this set and returns a reference to this set; the same as subtract().
QSet & QSet::operator<<(const T &t)
Inserts the value t into the set and returns a reference to this set; the same as insert(). The calls can be chained.
QSet & QSet::operator|=(const QSet &o)
Inserts all values of the set o into this set and returns a reference to this set; the same as unite().
bool QSet::remove(const T &t)
Removes the value t from the set. Returns true if the value was present; otherwise returns false.
See also insert() and clear().
template <typename Pred>qsizetype QSet::removeIf(Pred p)
Removes all values for which p returns true and returns the number of values removed. The predicate is called with an iterator to the value.
See also remove().
void QSet::reserve(qsizetype n)
Prepares the set for holding at least n values without rehashing.
qsizetype QSet::size() const
Returns the number of values in the set.
See also isEmpty() and count().
void QSet::squeeze()
Shrinks the hash table to the smallest size that holds the values.
See also capacity().
QSet & QSet::subtract(const QSet &o)
Removes all values of the set o from this set and returns a reference to this set.
See also unite() and intersect().
QList<T> QSet::toList() const
Returns the values of the set as a QList; the same as values().
QSet & QSet::unite(const QSet &o)
Inserts all values of the set o into this set and returns a reference to this set.
See also intersect() and subtract().
QList<T> QSet::values() const
Returns the values of the set as a QList, in unspecified order.
See also toList().