QSet 類

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

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

注意: 此類的所有函數 可重入 .

公共類型

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

公共函數

QSet ()
QSet (std::initializer_list<T> list )
QSet::const_iterator begin () const
QSet::iterator begin ()
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> & other ) const
int count () const
QSet::const_reverse_iterator crbegin () const
QSet::const_reverse_iterator crend () const
bool empty () const
QSet::const_iterator end () const
QSet::iterator end ()
QSet::iterator erase (QSet::const_iterator pos )
QSet::iterator erase (QSet::iterator pos )
QSet::const_iterator find (const T & value ) const
QSet::iterator find (const T & value )
QSet::iterator insert (const T & value )
QSet<T> & intersect (const QSet<T> & other )
bool intersects (const QSet<T> & other ) 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 )
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 )

靜態公共成員

QSet<T> fromList (const QList<T> & list )
QDataStream & operator<< (QDataStream & out , const QSet<T> & set )
QDataStream & operator>> (QDataStream & in , QSet<T> & set )

詳細描述

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

QSet <T> is one of Qt's generic 容器類 . It stores values in an unspecified order and provides very fast lookup of the values. Internally, QSet <T> is implemented as a QHash .

這裏是範例 QSet with QString 值:

QSet<QString> set;
					

要將值插入集,使用 insert ():

set.insert("one");
set.insert("three");
set.insert("seven");
					

將項插入集的另一方式是使用 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())
    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 ,還可以使用 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 可賦值數據類型 。例如,無法存儲 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 ().

在內部, 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 .

成員類型文檔編製

typedef QSet:: ConstIterator

Qt 樣式同義詞 QSet::const_iterator .

typedef QSet:: Iterator

Qt 樣式同義詞 QSet::iterator .

該 typedef 在 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> .

警告: 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 隱式共享迭代器問題 .

該 typedef 在 Qt 5.6 引入。

另請參閱 QSet::rbegin (), QSet::rend (), QSet::reverse_iterator ,和 QSet::const_iterator .

typedef QSet:: difference_type

Typedef for const ptrdiff_t. Provided for STL compatibility.

typedef QSet:: key_type

typedef 對於 T。為兼容 STL 提供。

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

警告: 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 隱式共享迭代器問題 .

該 typedef 在 Qt 5.6 引入。

另請參閱 QSet::rbegin (), QSet::rend (), QSet::const_reverse_iterator ,和 QSet::iterator .

typedef QSet:: size_type

typedef 對於 int。為兼容 STL 提供。

typedef QSet:: value_type

typedef 對於 T。為兼容 STL 提供。

成員函數文檔編製

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

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

Same as unite( other ).

另請參閱 operator| (), operator&= (),和 operator-= ().

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

This function is only available if the program is being compiled in C++11 mode.

該函數在 Qt 5.1 引入。

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.

該函數在 Qt 4.2 引入。

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

該函數在 Qt 5.0 引入。

另請參閱 begin () 和 cend ().

QSet::const_iterator QSet:: cend () const

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

該函數在 Qt 5.0 引入。

另請參閱 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 ().

該函數在 Qt 4.2 引入。

另請參閱 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 .

該函數在 Qt 4.6 引入。

另請參閱 insert (), remove (),和 find ().

int QSet:: count () const

如同 size ().

QSet::const_reverse_iterator QSet:: crbegin () const

返迴常量 STL-style reverse iterator pointing to the first item in the set, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 begin (), rbegin (),和 rend ().

QSet::const_reverse_iterator QSet:: crend () const

返迴常量 STL-style reverse iterator pointing to one past the last item in the set, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 end (), rend (),和 rbegin ().

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.

該函數在 Qt 4.2 引入。

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.

該函數在 Qt 5.7 引入。

另請參閱 remove () 和 find ().

QSet::iterator QSet:: erase ( QSet::iterator pos )

這是重載函數。

該函數在 Qt 4.2 引入。

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

該函數在 Qt 4.2 引入。

另請參閱 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 ().

該函數在 Qt 4.2 引入。

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

返迴新的 QSet object containing the data contained in list 。由於 QSet doesn't allow duplicates, the resulting QSet might be smaller than the list ,因為 QList can contain duplicates.

範例:

QStringList list;
list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia";
QSet<QString> set = QSet<QString>::fromList(list);
set.contains("Julia");  // returns true
set.contains("Mike");   // returns true
set.size();             // returns 2
					

另請參閱 toList () 和 QList::toSet ().

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

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 .

該函數在 Qt 5.6 引入。

另請參閱 contains () 和 intersect ().

bool QSet:: isEmpty () const

返迴 true 若集不包含元素;否則返迴 false。

另請參閱 size ().

QSet::reverse_iterator QSet:: rbegin ()

返迴 STL-style reverse iterator pointing to the first item in the set, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 begin (), crbegin (),和 rend ().

QSet::const_reverse_iterator QSet:: rbegin () const

這是重載函數。

該函數在 Qt 5.6 引入。

bool QSet:: remove (const T & value )

刪除任何齣現的項 value from the set. Returns true if an item was actually removed; otherwise returns false .

另請參閱 contains () 和 insert ().

QSet::reverse_iterator QSet:: rend ()

返迴 STL-style reverse iterator pointing to one past the last item in the set, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 end (), crend (),和 rbegin ().

QSet::const_reverse_iterator QSet:: rend () const

這是重載函數。

該函數在 Qt 5.6 引入。

void QSet:: reserve ( int 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 ().

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

QList < T > QSet:: toList () const

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

範例:

QSet<QString> set;
set << "red" << "green" << "blue" << ... << "black";
QList<QString> list = set.toList();
qSort(list);
					

另請參閱 fromList () 和 QList::fromSet ().

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.

這如同 toList ().

另請參閱 fromList () 和 QList::fromSet ().

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

相關非成員

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

寫入 set 到流 out .

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

另請參閱 QDataStream 運算符格式 .

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

讀取集從流 in into set .

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

另請參閱 QDataStream 運算符格式 .