QSet 类

template <typename T> class QSet

QSet 类是提供基于哈希表的集的模板类。 更多...

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

注意: 此类的所有函数 可重入 .

公共类型

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

公共函数

QSet ()
QSet (std::initializer_list<T> list )
QSet (InputIterator first , InputIterator last )
QSet::const_iterator begin () const
QSet::iterator begin ()
qsizetype 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> & other ) const
qsizetype count () const
bool empty () const
QSet::const_iterator end () const
QSet::iterator end ()
QSet::iterator erase (QSet::const_iterator pos )
QSet::const_iterator find (const T & value ) const
QSet::iterator find (const T & value )
QSet::iterator insert (const T & value )
QSet::iterator insert (QSet::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 )
void reserve (qsizetype size )
qsizetype size () const
void squeeze ()
QSet<T> & subtract (const QSet<T> & other )
void swap (QSet<T> & other )
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 )
qsizetype erase_if (QSet<T> & set , Predicate pred )
QDataStream & operator<< (QDataStream & out , const QSet<T> & set )
QDataStream & operator>> (QDataStream & in , QSet<T> & set )

详细描述

QSet<T> 是一种 Qt 一般 容器类 。它以未指定次序存储值并提供非常快的值查找。在内部,QSet<T> 被实现为 QHash .

这里是 QSet 范例具有 QString 值:

QSet<QString> set;
					

要将值插入集,使用 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";
					

要测试项是否属于集,使用 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 风格迭代器 ( QSetIterator and QMutableSetIterator ) 和 STL 样式迭代器 ( 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(); != 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 可赋值数据类型 。例如,无法存储 QWidget 作为值;取而代之,存储 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.

另请参阅 QSetIterator , QMutableSetIterator , QHash ,和 QMap .

成员类型文档编制

QSet:: ConstIterator

Qt 样式同义词 QSet::const_iterator .

QSet:: Iterator

Qt 样式同义词 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 对于 T。为兼容 STL 提供。

QSet:: pointer

Typedef for T *. Provided for STL compatibility.

QSet:: reference

Typedef for T &. Provided for STL compatibility.

QSet:: size_type

typedef 对于 int。为兼容 STL 提供。

QSet:: value_type

typedef 对于 T。为兼容 STL 提供。

成员函数文档编制

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.

另请参阅 insert ().

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

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

如同 unite( other ) .

另请参阅 operator| (), operator&= (),和 operator-= ().

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

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

返回新的 QSet that is the union of this set and the other set.

另请参阅 unite (), operator|= (), operator& (),和 operator- ().

QSet:: QSet ()

构造空集。

另请参阅 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> 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 .

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

QSet::const_iterator QSet:: begin () const

返回常量 STL 样式迭代器 positioned at the first item in the set.

另请参阅 constBegin () 和 end ().

QSet::iterator QSet:: begin ()

这是重载函数。

Returns a non-const STL 样式迭代器 positioned at the first item in the set.

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

另请参阅 reserve () 和 squeeze ().

QSet::const_iterator QSet:: cbegin () const

返回常量 STL 样式迭代器 positioned at the first item in the set.

另请参阅 begin () 和 cend ().

QSet::const_iterator QSet:: cend () const

返回常量 STL 样式迭代器 pointing to the imaginary item after the last item in the set.

另请参阅 cbegin () 和 end ().

void QSet:: clear ()

从集移除所有元素。

另请参阅 remove ().

QSet::const_iterator QSet:: constBegin () const

返回常量 STL 样式迭代器 positioned at the first item in the set.

另请参阅 begin () 和 constEnd ().

QSet::const_iterator QSet:: constEnd () const

返回常量 STL 样式迭代器 pointing to the imaginary item after the last item in the set.

另请参阅 constBegin () 和 end ().

QSet::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 ,函数返回 constEnd ().

另请参阅 find () 和 contains ().

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

返回 true 若集包含项 value ;否则返回 false。

另请参阅 insert (), remove (),和 find ().

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

返回 true 若集包含的所有项来自 other 集;否则返回 false .

另请参阅 insert (), remove (),和 find ().

qsizetype QSet:: count () const

如同 size ().

bool QSet:: empty () const

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

QSet::const_iterator QSet:: end () const

返回常量 STL 样式迭代器 positioned at the imaginary item after the last item in the set.

另请参阅 constEnd () 和 begin ().

QSet::iterator QSet:: end ()

这是重载函数。

Returns a non-const STL 样式迭代器 pointing to the imaginary item after the last item in the set.

QSet::iterator QSet:: erase ( QSet::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.

不像 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.

注意: 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.

另请参阅 remove () 和 find ().

QSet::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 ,函数返回 constEnd ().

另请参阅 constFind () 和 contains ().

QSet::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 ,函数返回 end ().

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

另请参阅 operator<< (), remove (),和 contains ().

[since 6.1] QSet::iterator QSet:: insert ( QSet::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 被忽略。

This function is provided for compatibility with the STL.

该函数在 Qt 6.1 引入。

另请参阅 operator<< (), remove (),和 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.

另请参阅 intersects (), operator&= (), unite (),和 subtract ().

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

返回 true if this set has at least one item in common with other .

另请参阅 contains () 和 intersect ().

bool QSet:: isEmpty () const

返回 true 若集不包含元素;否则返回 false。

另请参阅 size ().

bool QSet:: remove (const T & value )

删除任何出现的项 value from the set. Returns true if an item was actually removed; otherwise returns false .

另请参阅 contains () 和 insert ().

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.

另请参阅 squeeze () 和 capacity ().

qsizetype QSet:: size () const

Returns the number of items in the set.

另请参阅 isEmpty () 和 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.

另请参阅 reserve () 和 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.

另请参阅 operator-= (), unite (),和 intersect ().

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

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

QSet < T > &QSet:: unite (const 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.

另请参阅 operator|= (), intersect (),和 subtract ().

QList < T > QSet:: values () const

返回新的 QList containing the elements in the set. The order of the elements in the QList is undefined.

注意: Since Qt 5.14, range constructors are available for Qt's generic 容器类 and should be used in place of this method.

此函数创建新列表,按 线性时间 . The time and memory use that entails can be avoided by iterating from constBegin () 到 constEnd ().

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

返回 true other set 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

返回新的 QSet that is the intersection of this set and the other set.

另请参阅 intersect (), operator&= (), operator| (),和 operator- ().

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

如同 intersect( other ) .

另请参阅 operator& (), operator|= (),和 operator-= ().

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

这是重载函数。

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

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

返回新的 QSet that is the set difference of this set and the other set, i.e., this set - other set.

另请参阅 subtract (), operator-= (), operator| (),和 operator& ().

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

如同 subtract( other ) .

另请参阅 operator- (), operator|= (),和 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.

另请参阅 remove ().

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

返回 true other 集等于此集;否则返回 false .

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

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

另请参阅 operator!= ().

相关非成员

[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.

该函数在 Qt 6.1 引入。

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

写入 set 到流 out .

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

另请参阅 QDataStream 运算符格式 .

template <typename T> QDataStream & operator>> ( QDataStream & in , QSet < T > & set )

读取集从流 in into set .

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

另请参阅 QDataStream 运算符格式 .