CopperSpice API  1.9.1
QFlatMap< Key, Val, C >::iterator Class Reference

STL style iterator for QFlatMap. More...

Public Typedefs

using iterator_category = std::bidirectional_iterator_tag
 

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-- ()
 
iterator operator-- (int)
 
iterator & operator-= (size_type n)
 
Val * operator-> () const
 
bool operator== (iterator other) const
 
std::pair< Key, Val > & pair () const
 
Val & value () const
 

Friends

class QFlatMap< Key, Val, C >
 

Detailed Description

template<typename Key, typename Val, typename C>
class QFlatMap< Key, Val, C >::iterator

The QFlatMap::iterator class provides an STL style iterator for QFlatMap. QFlatMap features both STL style iterators and Java style iterators.

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

After construction you must initialize the iterator using a method like QFlatMap::begin(), QFlatMap::end(), or QFlatMap::find() before you can start iterating.

The following example prints all of the (key, value) pairs stored in the flat map.

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

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

for (iter = flatMap.begin(); iter != flatMap.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 != flatMap.end()) {
if (iter.key().startsWith("_")) {
iter = flatMap.erase(iter);
} else {
++iter;
}
}

The call to QFlatMap::erase() removes the item pointed to by the iterator in the flat map and returns an iterator to the next item. Here is another way of removing an item while iterating:

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

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

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

Member Typedef Documentation

template<typename Key , typename Val , typename C >
QFlatMap< Key, Val, C >::iterator::iterator_category

A synonym for std::bidirectional_iterator_tag indicating this iterator is a bidirectional iterator.

Constructor & Destructor Documentation

template<typename Key , typename Val , typename C >
QFlatMap< Key, Val, C >::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
QFlatMap::begin(), QFlatMap::end()

Method Documentation

template<typename Key , typename Val , typename C >
const Key & QFlatMap< Key, Val, C >::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 QFlatMap::erase() followed by QFlatMap::insert() or QFlatMap::insertMulti().

See also
value()
template<typename Key , typename Val , typename C >
bool QFlatMap< Key, Val, C >::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 C >
Val & QFlatMap< Key, Val, C >::iterator::operator* ( ) const
inline

Equivalent to calling value().

See also
key()
template<typename Key , typename Val , typename C >
iterator QFlatMap< Key, Val, C >::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 C >
iterator & QFlatMap< Key, Val, C >::iterator::operator++ ( )
inline

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

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

See also
operator--()
template<typename Key , typename Val , typename C >
iterator QFlatMap< Key, Val, C >::iterator::operator++ ( int  )
inline

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

template<typename Key , typename Val , typename C >
iterator & QFlatMap< Key, Val, C >::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 C >
iterator QFlatMap< Key, Val, C >::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 C >
iterator & QFlatMap< Key, Val, C >::iterator::operator-- ( )
inline

The prefix – operator (–i) makes the preceding item current and returns an iterator pointing to the new current item.

Calling this method on QFlatMap::begin() leads to undefined behavior.

See also
operator++()
template<typename Key , typename Val , typename C >
iterator QFlatMap< Key, Val, C >::iterator::operator-- ( int  )
inline

The postfix – operator (i–) makes the preceding item current and returns an iterator pointing to the previously current item.

template<typename Key , typename Val , typename C >
iterator & QFlatMap< Key, Val, C >::iterator::operator-= ( size_type  n)
inline

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

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

Returns a pointer to the current item's value.

See also
value()
template<typename Key , typename Val , typename C >
bool QFlatMap< Key, Val, C >::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 C >
std::pair< Key, Val > & QFlatMap< Key, Val, C >::iterator::pair ( ) const
inline

Returns a pair consisting of the key and the value.

template<typename Key , typename Val , typename C >
Val & QFlatMap< Key, Val, C >::iterator::value ( ) const
inline

Returns a reference to the value of the current item. 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*()