CopperSpice API  1.7.2
QHash< Key, Val, Hash, KeyEqual >::iterator Class Reference

STL style non-const iterator for QHash. More...

Public Methods

 iterator () = default
 
const Key & key () const
 
bool operator!= (iterator other) const
 
Val & operator* () const
 
iterator operator+ (size_type n) const
 
iterator & operator++ ()
 
iterator operator++ (int)
 
iterator & operator+= (size_type n)
 
iterator operator- (size_type n) const
 
iterator & operator-= (size_type n)
 
Val * operator-> () const
 
bool operator== (iterator other) const
 
std::pair< const Key, Val > & pair () const
 
Val & value () const
 

Friends

class QHash< Key, Val, Hash, KeyEqual >
 

Detailed Description

template<typename Key, typename Val, typename Hash, typename KeyEqual>
class QHash< Key, Val, Hash, KeyEqual >::iterator

The QHash::iterator class provides an STL style non-const iterator for QHash. QHash features both STL style iterators and Java style iterators.

QHash::const_iterator allows you to iterate over a QHash. If you want to modify the QHash as you iterate over it use QHash::iterator instead.

After construction you must initialize the iterator using a method like QHash::constBegin(), QHash::constEnd(), or QHash::find() before you can start iterating. The following is an example which prints all of the (key, value) pairs in the container.

hash.insert("January", 1);
hash.insert("February", 2);
...
hash.insert("December", 12);
for (auto iter = map.constBegin(); iter != map.constEnd(); ++iter) {
cout << iter.key() << ": " << iter.value() << endl;
}

Unlike QMap which orders its items by key, QHash stores its items in an arbitrary order.

The following is an example which increments every value stored in the QHash by 2:

for (iter = hash.begin(); iter != hash.end(); ++iter) {
iter.value() += 2;
}

Here is an example which removes all the items whose key is a string that starts with an underscore character:

while (iter != hash.end()) {
if (iter.key().startsWith("_")) {
iter = hash.erase(iter);
} else {
++iter;
}
}

The call to QHash::erase() removes the item pointed to by the iterator from the hash, and returns an iterator to the next item. Here is another way of removing an item while iterating:

while (iter != hash.end()) {
++i;
if (prev.key().startsWith("_")) {
hash.erase(prev);
}
}

It might be tempting to write code like the following. This code will potentially crash in ++iter, because iter is invalid after the call to erase().

// INCORRECT
while (iter != hash.end()) {
if (iter.key().startsWith("_")) {
hash.erase(iter);
}
++iter;
}
See also
QHash::const_iterator

Constructor & Destructor Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::iterator::iterator ( )
default

Constructs an uninitialized iterator.

Methods like key(), value(), and operator++() must not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it.

See also
QHash::begin(), QHash::end()

Method Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Key & QHash< Key, Val, Hash, KeyEqual >::iterator::key ( ) const
inline

Returns the current item's key as a const reference.

There is no direct way of changing an item's key through an iterator, although it can be done by calling QHash::erase() followed by QHash::insert() or QHash::insertMulti().

See also
value()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::iterator::operator!= ( iterator  other) const
inline

Returns true if other points to a different item than this iterator, otherwise it returns false.

See also
operator==()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val & QHash< Key, Val, Hash, KeyEqual >::iterator::operator* ( ) const
inline

Equivalent to calling value().

See also
key()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::iterator::operator+ ( size_type  n) const
inline

Returns an iterator to the item at n positions forward from this iterator. If n is negative the iterator goes backward.

This operation can be slow for large values of n.

See also
operator-()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator & QHash< Key, Val, Hash, KeyEqual >::iterator::operator++ ( )
inline

The prefix ++ operator (++i) advances the iterator to the next item in the hash and returns an iterator to the new current item.

Calling this method on QHash::end() leads to undefined behavior.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::iterator::operator++ ( int  )
inline

The postfix ++ operator (i++) advances the iterator to the next item in the hash and returns an iterator to the previously current item.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator & QHash< Key, Val, Hash, KeyEqual >::iterator::operator+= ( size_type  n)
inline

Advances the iterator by n items. If n is negative the iterator goes backward.

See also
operator-=(), operator+()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::iterator::operator- ( size_type  n) const
inline

Returns an iterator to the item at n positions backward from this iterator. If n is negative the iterator goes forward.

This operation can be slow for large values of n.

See also
operator+()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator & QHash< Key, Val, Hash, KeyEqual >::iterator::operator-= ( size_type  n)
inline

Makes the iterator go back by n items. nIf n is negative the iterator goes forward.

See also
operator+=(), operator-()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val * QHash< Key, Val, Hash, KeyEqual >::iterator::operator-> ( ) const
inline

Returns a pointer to the current item's value.

See also
value()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::iterator::operator== ( iterator  other) const
inline

Returns true if other points to the same item as this iterator, otherwise it returns false.

See also
operator!=()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
std::pair< const Key, Val > & QHash< Key, Val, Hash, KeyEqual >::iterator::pair ( ) const
inline

Returns the current item's key and value as a pair.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val & QHash< Key, Val, Hash, KeyEqual >::iterator::value ( ) const
inline

Returns a reference to the value for the item with the given key.You can change the value of an item by using value() on the left side of an assignment.

if (i.key() == "Hello") {
i.value() = "Bonjour";
}
See also
key(), operator*()