
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQSet class is a template class that provides a hash-table-based set.More...
| Header: | #include <QSet> |
Note: All functions in this class arereentrant.
| class | const_iterator |
| class | iterator |
| typedef | ConstIterator |
| typedef | Iterator |
| typedef | const_pointer |
| typedef | const_reference |
| typedef | difference_type |
| typedef | key_type |
| typedef | pointer |
| typedef | reference |
| typedef | size_type |
| typedef | value_type |
| QSet() | |
| QSet(const QSet<T> & other) | |
| const_iterator | begin() const |
| iterator | begin() |
| int | capacity() const |
| void | clear() |
| const_iterator | constBegin() const |
| const_iterator | constEnd() const |
| const_iterator | constFind(const T & value) const |
| bool | contains(const T & value) const |
| bool | contains(const QSet<T> & other) const |
| int | count() const |
| bool | empty() const |
| const_iterator | end() const |
| iterator | end() |
| iterator | erase(iterator pos) |
| const_iterator | find(const T & value) const |
| iterator | find(const T & value) |
| const_iterator | insert(const T & value) |
| QSet<T> & | intersect(const QSet<T> & other) |
| bool | isEmpty() const |
| bool | remove(const T & value) |
| 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) |
| QSet<T> & | operator=(const QSet<T> & other) |
| QSet<T> & | operator=(QSet<T> && other) |
| 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) |
TheQSet class is a template class that provides a hash-table-based set.
QSet<T> is one of Qt's genericcontainer classes. It stores values in an unspecified order and provides very fast lookup of the values. Internally,QSet<T> is implemented as aQHash.
Here's an exampleQSet withQString values:
To insert a value into the set, useinsert():
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, usecontains():
if (!set.contains("ninety-nine"))...
If you want to navigate through all the values stored in aQSet, you can use an iterator.QSet supports bothJava-style iterators (QSetIterator andQMutableSetIterator) andSTL-style iterators (QSet::iterator andQSet::const_iterator). Here's how to iterate over aQSet<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 aQMap.
To navigate through aQSet, you can also useforeach:
Items can be removed from the set usingremove(). There is also aclear() function that removes all items.
QSet's value data type must be anassignable data type. You cannot, for example, store aQWidget as a value; instead, store aQWidget *. In addition, the type must provideoperator==(), and there must also be a globalqHash() function that returns a hash value for an argument of the key's type. See theQHash documentation for a list of types supported byqHash().
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 callingreserve(), if you already know approximately how many elements theQSet will contain, but this isn't necessary to obtain good performance. You can also callcapacity() to retrieve the hash table's size.
See alsoQSetIterator,QMutableSetIterator,QHash, andQMap.
Qt-style synonym forQSet::const_iterator.
Qt-style synonym forQSet::iterator.
This typedef was introduced in Qt 4.2.
Typedef for const T *. Provided for STL compatibility.
Typedef for const T &. Provided for STL compatibility.
Typedef for const ptrdiff_t. Provided for STL compatibility.
Typedef for T. Provided for STL compatibility.
Typedef for T *. Provided for STL compatibility.
Typedef for T &. Provided for STL compatibility.
Typedef for int. Provided for STL compatibility.
Typedef for T. Provided for STL compatibility.
Constructs an empty set.
See alsoclear().
Constructs a copy ofother.
This operation occurs inconstant time, becauseQSet isimplicitly shared. This makes returning aQSet from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takeslinear time.
See alsooperator=().
Returns a const STL-style iterator positioned at the first item in the set.
See alsoconstBegin() andend().
This is an overloaded function.
Returns a non-const STL-style iterator positioned at the first item in the set.
This function was introduced in Qt 4.2.
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 tuningQSet'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, callsize().
See alsoreserve() andsqueeze().
Removes all elements from the set.
See alsoremove().
Returns a const STL-style iterator positioned at the first item in the set.
See alsobegin() andconstEnd().
Returns a const STL-style iterator pointing to the imaginary item after the last item in the set.
See alsoconstBegin() andend().
Returns a const iterator positioned at the itemvalue in the set. If the set contains no itemvalue, the function returnsconstEnd().
This function was introduced in Qt 4.2.
Returns true if the set contains itemvalue; otherwise returns false.
See alsoinsert(),remove(), andfind().
Returns true if the set contains all items from theother set; otherwise returns false.
This function was introduced in Qt 4.6.
See alsoinsert(),remove(), andfind().
Same assize().
Returns true if the set is empty. This function is provided for STL compatibility. It is equivalent toisEmpty().
Returns a const STL-style iterator positioned at the imaginary item after the last item in the set.
See alsoconstEnd() andbegin().
This is an overloaded function.
Returns a non-const STL-style iterator pointing to the imaginary item after the last item in the set.
This function was introduced in Qt 4.2.
Removes the item at the iterator positionpos from the set, and returns an iterator positioned at the next item in the set.
Unlikeremove(), this function never causesQSet 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.
This function was introduced in Qt 4.2.
Returns a const iterator positioned at the itemvalue in the set. If the set contains no itemvalue, the function returnsconstEnd().
This function was introduced in Qt 4.2.
See alsoconstFind() andcontains().
This is an overloaded function.
Returns a non-const iterator positioned at the itemvalue in the set. If the set contains no itemvalue, the function returnsend().
This function was introduced in Qt 4.2.
[static]QSet<T> QSet::fromList(constQList<T> & list)Returns a newQSet object containing the data contained inlist. SinceQSet doesn't allow duplicates, the resultingQSet might be smaller than thelist, becauseQList can contain duplicates.
Example:
QStringList list;list<<"Julia"<<"Mike"<<"Mike"<<"Julia"<<"Julia";QSet<QString> set=QSet<QString>::fromList(list);set.contains("Julia");// returns trueset.contains("Mike");// returns trueset.size();// returns 2
See alsotoList() andQList::toSet().
Inserts itemvalue into the set, ifvalue isn't already in the set, and returns an iterator pointing at the inserted item.
See alsooperator<<(),remove(), andcontains().
Removes all items from this set that are not contained in theother set. A reference to this set is returned.
See alsooperator&=(),unite(), andsubtract().
Returns true if the set contains no elements; otherwise returns false.
See alsosize().
Removes any occurrence of itemvalue from the set. Returns true if an item was actually removed; otherwise returns false.
See alsocontains() andinsert().
Ensures that the set's internal hash table consists of at leastsize buckets.
This function is useful for code that needs to build a huge set and wants to avoid repeated reallocation. For example:
Ideally,size should be slightly more than the maximum number of elements expected in the set.size doesn't have to be prime, becauseQSet will use a prime number internally anyway. Ifsize is an underestimate, the worst that will happen is that theQSet 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 alsosqueeze() andcapacity().
Returns the number of items in the set.
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 tuningQSet's memory usage. In general, you will rarely ever need to call this function.
See alsoreserve() andcapacity().
Removes all items from this set that are contained in theother set. Returns a reference to this set.
See alsooperator-=(),unite(), andintersect().
Swaps setother with this set. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
Returns a newQList containing the elements in the set. The order of the elements in theQList is undefined.
Example:
QSet<QString> set;set<<"red"<<"green"<<"blue"<<...<<"black";QList<QString> list= set.toList();qSort(list);
See alsofromList(),QList::fromSet(), andqSort().
Each item in theother set that isn't already in this set is inserted into this set. A reference to this set is returned.
See alsooperator|=(),intersect(), andsubtract().
Returns a newQList containing the elements in the set. The order of the elements in theQList is undefined.
This is the same astoList().
See alsofromList(),QList::fromSet(), andqSort().
Returns true if theother 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 implementoperator==().
See alsooperator==().
Returns a newQSet that is the intersection of this set and theother set.
See alsointersect(),operator&=(),operator|(), andoperator-().
Same as intersect(other).
See alsooperator&(),operator|=(), andoperator-=().
This is an overloaded function.
Same as intersect(other), if we considerother to be a set that contains the singletonvalue.
Returns a newQSet that is the union of this set and theother set.
See alsounite(),operator|=(),operator&(), andoperator-().
Same as unite(other).
See alsooperator|(),operator&=(), andoperator-=().
Inserts a new itemvalue and returns a reference to the set. Ifvalue already exists in the set, the set is left unchanged.
See alsoinsert().
Returns a newQSet that is the set difference of this set and theother set, i.e., this set -other set.
See alsosubtract(),operator-=(),operator|(), andoperator&().
Same as subtract(other).
See alsooperator-(),operator|=(), andoperator&=().
Removes the occurrence of itemvalue from the set, if it is found, and returns a reference to the set. If thevalue is not contained the set, nothing is removed.
See alsoremove().
Inserts a new itemvalue and returns a reference to the set. Ifvalue already exists in the set, the set is left unchanged.
See alsoinsert().
Assigns theother set to this set and returns a reference to this set.
Returns true if theother set is 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 implementoperator==().
See alsooperator!=().
Returns a newQSet that is the union of this set and theother set.
See alsounite(),operator|=(),operator&(), andoperator-().
Same as unite(other).
See alsooperator|(),operator&=(), andoperator-=().
Inserts a new itemvalue and returns a reference to the set. Ifvalue already exists in the set, the set is left unchanged.
See alsoinsert().
Writes theset to streamout.
This function requires the value type to implementoperator<<().
See alsoFormat of the QDataStream operators.
Reads a set from streamin intoset.
This function requires the value type to implementoperator>>().
See alsoFormat of the QDataStream operators.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.