CopperSpice API  1.9.1
QSet< T > Class Template Reference

Template class which provides a set implemented as a hash. More...

Classes

class  const_iterator
 Provides an STL style const iterator for QSet More...
 
class  iterator
 Provides an STL style iterator for QSet More...
 

Public Typedefs

using allocator_type = typename std::unordered_set< T, Hash >::allocator_type
 
using const_iterator = typename std::unordered_set< T, Hash >::const_iterator
 
using const_pointer = typename std::unordered_set< T, Hash >::const_pointer
 
using const_reference = typename std::unordered_set< T, Hash >::const_reference
 
using difference_type = typename std::unordered_set< T, Hash >::difference_type
 
using hasher = typename std::unordered_set< T, Hash >::hasher
 
using iterator = typename std::unordered_set< T, Hash >::iterator
 
using Java_Iterator = QSetIterator< T >
 
using Java_MutableIterator = QMutableSetIterator< T >
 
using key_equal = typename std::unordered_set< T, Hash >::key_equal
 
using key_type = typename std::unordered_set< T, Hash >::key_type
 
using pointer = typename std::unordered_set< T, Hash >::pointer
 
using reference = typename std::unordered_set< T, Hash >::reference
 
using size_type = typename std::unordered_set< T, Hash >::difference_type
 
using value_type = typename std::unordered_set< T, Hash >::value_type
 

Public Methods

 QSet () = default
 
 QSet (const QSet< T > &other) = default
 
template<class Input_Iterator >
 QSet (Input_Iterator first, Input_Iterator last)
 
 QSet (QSet< T > &&other) = default
 
 QSet (std::initializer_list< T > args)
 
iterator begin ()
 
const_iterator begin () const
 
size_type capacity () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_iterator constFind (const T &value) const
 
bool contains (const QSet< T > &other) const
 
bool contains (const T &value) const
 
size_type count () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
size_type erase (const key_type &key)
 
iterator erase (const_iterator first, const_iterator last)
 
iterator erase (const_iterator pos)
 
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 intersects (const QSet< T > &other) const
 
bool isEmpty () 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) = default
 
QSet< T > & operator= (QSet< T > &&other) = default
 
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)
 
bool remove (const T &value)
 
void reserve (size_type size)
 
size_type 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
 

Static Public Methods

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

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &stream, const QSet< T > &set)
 
QDataStreamoperator>> (QDataStream &stream, QSet< T > &set)
 

Detailed Description

template<class T>
class QSet< T >

The QSet class is a template class which provides a set implemented as a hash. This container stores values in an unspecified order and provides very fast lookup of the values.

For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.

Constructors

The following code is a simple example showing how to declare a QSet.

QSet<QString> set; // stores QString values

Accessing Elements

To insert a value into the set use insert() as shown below.

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 is present in a set use the contains() method.

if (! set.contains("ninety-nine")) {
// not found
}

Basic Operations

Items can be removed from the set using remove(). There is also a clear() method that removes all items.

Iterators

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

The following shows how to iterate over a QSet<QWidget *> using a Java style iterator.

while (i.hasNext()) {
qDebug() << i.next();
}

Here is the same code using an STL style iterator.

while (i != set.constEnd()) {
qDebug() << *i;
++i;
}

QSet is unordered so the iterator sequence can not be assumed to be predictable. If ordering by key is required use a QMap.

To navigate through a QSet you can use a range based for loop.

for (const QString &value : set) {
qDebug() << value;
}

Constraints on type T

The QSet value type T should be Default Constructible and Copy Constructible to work with all methods of this class. Data types like QObject are not copy constructible and therefore should never be stored in a container. You can however use a pointer to a QObject or any subclass as the type T.

The type T must provide an operator==() method and there needs to be a global qHash() function which returns a hash value for an argument of the key's type. Refer to the QHash documentation for a list of types supported by qHash().

Additional Information

The QSet 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 is not necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

See also
QFlatMap, QHash, QMap

Member Typedef Documentation

template<class T >
QSet< T >::allocator_type

Typedef for allocator used by the container.

template<class T >
QSet< T >::const_iterator

Typedef for std::unordered_set<T>::const_iterator.

template<class T >
QSet< T >::const_pointer

Typedef for const T *.

template<class T >
QSet< T >::const_reference

Typedef for const T &;.

template<class T >
QSet< T >::difference_type

Typedef for const ptrdiff_t.

template<class T >
QSet< T >::hasher

Typedef for the functor used to hash keys.

template<class T >
QSet< T >::iterator

Typedef for std::unordered_set<T>::iterator.

template<class T >
QSet< T >::Java_Iterator

Typedef for the java style const iterator.

See also
QSetIterator()
template<class T >
QSet< T >::Java_MutableIterator

Typedef for the java style mutable iterator.

See also
QMutableSetIterator()
template<class T >
QSet< T >::key_equal

Typedef for the functor used to compare keys.

template<class T >
QSet< T >::key_type

Typedef for T.

template<class T >
QSet< T >::pointer

Typedef for T *.

template<class T >
QSet< T >::reference

Typedef for T &.

template<class T >
QSet< T >::size_type

Typedef for int.

template<class T >
QSet< T >::value_type

Typedef for T.

Constructor & Destructor Documentation

template<class T >
QSet< T >::QSet ( )
default

Constructs an empty set.

See also
clear()
template<class T >
QSet< T >::QSet ( const QSet< T > &  other)
default

Copy constructs a new QSet from other.

template<class T >
QSet< T >::QSet ( QSet< T > &&  other)
default

Move constructs a new QSet from other.

template<class T >
QSet< T >::QSet ( std::initializer_list< T >  args)
inline

Construct a set from the std::initializer_list specified by args.

template<class T >
template<class Input_Iterator >
QSet< T >::QSet ( Input_Iterator  first,
Input_Iterator  last 
)

Construct a set containing the elements from first to last.

Method Documentation

template<class T >
iterator QSet< T >::begin ( )
inline

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

template<class T >
const_iterator QSet< T >::begin ( ) const
inline

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

See also
constBegin(), end()
template<class T >
size_type QSet< T >::capacity ( ) const
inline

Returns the number of buckets in the set's internal hash table.

The purpose of this method is to provide a means of fine tuning QSet's memory usage. In general, you will rarely ever need to call this method. If you want to know how many items are in the set, call size().

See also
reserve(), squeeze()
template<class T >
const_iterator QSet< T >::cbegin ( ) const
inline

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

See also
cend()
template<class T >
const_iterator QSet< T >::cend ( ) const
inline

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

See also
cbegin()
template<class T >
void QSet< T >::clear ( )
inline

Removes all elements from this QSet.

See also
remove()
template<class T >
const_iterator QSet< T >::constBegin ( ) const
inline

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

See also
constEnd()
template<class T >
const_iterator QSet< T >::constEnd ( ) const
inline

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

See also
constBegin()
template<class T >
const_iterator QSet< T >::constFind ( const T &  value) const
inline

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

See also
find(), contains()
template<class T >
bool QSet< T >::contains ( const QSet< T > &  other) const
inline

Returns true if this QSet contains all items from the other set, otherwise returns false.

See also
insert(), remove(), find()
template<class T >
bool QSet< T >::contains ( const T &  value) const
inline

Returns true if this QSet contains item value, otherwise returns false.

See also
insert(), remove(), find()
template<class T >
size_type QSet< T >::count ( ) const
inline

Equivalent to calling size().

template<class T >
bool QSet< T >::empty ( ) const
inline

Returns true if the QSet is empty.

template<class T >
iterator QSet< T >::end ( )
inline

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

template<class T >
const_iterator QSet< T >::end ( ) const
inline

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

See also
constEnd(), begin()
template<class T >
size_type QSet< T >::erase ( const key_type key)
inline

Removes every item matching key from this QSet. Returns how many items were removed.

template<class T >
iterator QSet< T >::erase ( const_iterator  first,
const_iterator  last 
)
inline

Removes the items between first and last.

template<class T >
iterator QSet< T >::erase ( const_iterator  pos)
inline

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 method never invalidates iterators. This means that it can safely be called while iterating and will not affect the order of items in the set.

See also
remove(), find()
template<class T >
iterator QSet< T >::find ( const T &  value)
inline

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

template<class T >
const_iterator QSet< T >::find ( const T &  value) const
inline

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

See also
constFind(), contains()
template<typename T >
QSet< T > QSet< T >::fromList ( const QList< T > &  list)
static

Returns a new QSet object containing the data contained in list. Since QSet does not allow duplicates, the resulting QSet might be smaller than the list, because QList can contain duplicates.

list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia";
set.contains("Julia"); // returns true
set.contains("Mike"); // returns true
set.size(); // returns 2
See also
toList(), QList::toSet()
template<class T >
iterator QSet< T >::insert ( const T &  value)
inline

Inserts item value into the set, if value is not already in the set, and returns an iterator pointing at the inserted item.

See also
operator<<(), remove(), contains()
template<class T >
QSet< T > & QSet< T >::intersect ( const QSet< T > &  other)
inline

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

See also
operator&=(), unite(), subtract()
template<class T >
bool QSet< T >::intersects ( const QSet< T > &  other) const
inline

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

template<class T >
bool QSet< T >::isEmpty ( ) const
inline

Returns true if the set contains no elements, otherwise returns false.

See also
size()
template<class T >
bool QSet< T >::operator!= ( const QSet< T > &  other) const
inline

Returns true if the other set is not equal to this set, otherwise returns false. Two sets are considered equal if they contain the same elements.

This method requires the type T to support operator==().

See also
operator==()
template<class T >
QSet< T > QSet< T >::operator& ( const QSet< T > &  other) const
inline

Returns a new QSet that is the intersection of this set and the other set.

See also
intersect(), operator&=(), operator|(), operator-()
template<class T >
QSet< T > & QSet< T >::operator&= ( const QSet< T > &  other)
inline

Equivalent to calling intersect(other).

See also
operator&(), operator|=(), operator-=()
template<class T >
QSet< T > & QSet< T >::operator&= ( const T &  value)
inline

Removes all elements from this QSet which do not match value.

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

Returns a new QSet which contains the elements from this QSet combined with elements in other.

See also
unite(), operator|=(), operator&(), operator-()
template<class T >
QSet< T > & QSet< T >::operator+= ( const QSet< T > &  other)
inline

Equivalent to calling unite(other).

See also
operator|(), operator&=(), operator-=()
template<class T >
QSet< T > & QSet< T >::operator+= ( const T &  value)
inline

Inserts a new item value and returns a reference to this QSet. If value already exists, the set is not modified.

See also
insert()
template<class T >
QSet< T > QSet< T >::operator- ( const QSet< T > &  other) const
inline

Returns a new QSet which contains all elements in this QSet and not in the other QSet.

See also
subtract(), operator-=(), operator|(), operator&()
template<class T >
QSet< T > & QSet< T >::operator-= ( const QSet< T > &  other)
inline

Equivalent to calling subtract(other).

See also
operator-(), operator|=(), operator&=()
template<class T >
QSet< T > & QSet< T >::operator-= ( const T &  value)
inline

Removes the occurrence of item value from this set. If the value is not contained in this QSet, nothing is removed. Returns a reference to the set.

See also
remove()
template<class T >
QSet< T > & QSet< T >::operator<< ( const T &  value)
inline

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

Copy assigns from other and returns a reference to this object.

template<class T >
QSet< T > & QSet< T >::operator= ( QSet< T > &&  other)
default

Move assigns from other and returns a reference to this object.

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

Returns true if the other set is equal to this set, otherwise returns false. Two sets are considered equal if they contain the same elements.

This method requires the type T to support operator==().

See also
operator!=()
template<class T >
QSet< T > QSet< T >::operator| ( const QSet< T > &  other) const
inline

Returns a new QSet that is the union of this set and the other set.

See also
unite(), operator|=(), operator&(), operator-()
template<class T >
QSet< T > & QSet< T >::operator|= ( const QSet< T > &  other)
inline

Equivalent to calling unite(other).

See also
operator|(), operator&=(), operator-=()
template<class T >
QSet< T > & QSet< T >::operator|= ( const T &  value)
inline

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()
template<class T >
bool QSet< T >::remove ( const T &  value)
inline

Removes any occurrence of item value from the set. Returns true if an item was actually removed. otherwise returns false.

See also
contains(), insert()
template<class T >
void QSet< T >::reserve ( size_type  size)
inline

Ensures the internal hash table consists of at least size buckets. This method is useful for code which needs to build a huge set and wants to avoid repeated reallocation.

Ideally the size should be slightly larger than the maximum number of elements expected in the set. The size does not need to be a prime number because QSet will round up to the nearest prime number. If size is underestimated the worst which can happen is QSet will be a bit slower. In general, you will rarely need to call this method. The QSet internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.

set.reserve(20000);
for (int i = 0; i < 20000; ++i) {
set.insert(values[i]);
}
See also
squeeze(), capacity()
template<class T >
size_type QSet< T >::size ( ) const
inline

Returns the number of items in the set.

See also
isEmpty(), count()
template<class T >
void QSet< T >::squeeze ( )
inline

Reduces the size of the set's internal hash table to save memory.

The purpose of this method is to provide a means to fine tune the internal data structure memory usage. In general, you will rarely ever need to call this method.

See also
reserve(), capacity()
template<class T >
QSet< T > & QSet< T >::subtract ( const QSet< T > &  other)
inline

Removes all items from this set that are contained in the other set. Returns a reference to this set.

See also
operator-=(), unite(), intersect()
template<class T >
void QSet< T >::swap ( QSet< T > &  other)
inline

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

template<class T >
QList< T > QSet< T >::toList ( ) const

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

set << "red" << "green" << "blue" << ... << "black";
QList<QString> list = set.toList();
std::sort(list.begin(), list.end());
See also
fromList(), QList::fromSet()
template<class T >
QSet< T > & QSet< T >::unite ( const QSet< T > &  other)
inline

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

See also
operator|=(), intersect(), subtract()
template<class T >
QList< T > QSet< T >::values ( ) const
inline

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

Equivalent to calling toList().

See also
fromList(), QList::fromSet()

Friends And Related Function Documentation

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

Writes the given set to the stream. Returns a reference to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream stream,
QSet< T > &  set 
)
related

Reads from the stream into the given set. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.