Qt 4.8
Public Types | Public Functions | Private Functions | Properties | Friends | List of all members
QMap< Key, T >::const_iterator Class Reference

The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap. More...

#include <qmap.h>

Public Types

typedef qptrdiff difference_type
 
typedef std::bidirectional_iterator_tag iterator_category
 A synonym for {std::bidirectional_iterator_tag} indicating this iterator is a bidirectional iterator. More...
 
typedef const T * pointer
 
typedef const T & reference
 
typedef T value_type
 

Public Functions

 const_iterator ()
 Constructs an uninitialized iterator. More...
 
 const_iterator (QMapData::Node *node)
 
 const_iterator (const iterator &o)
 Constructs a copy of other. More...
 
const Key & key () const
 Returns the current item's key. More...
 
 operator QMapData::Node * () const
 
bool operator!= (const const_iterator &o) const
 Returns true if other points to a different item than this iterator; otherwise returns false. More...
 
const T & operator* () const
 Returns the current item's value. More...
 
const_iterator operator+ (int j) const
 Returns an iterator to the item at j positions forward from this iterator. More...
 
const_iteratoroperator++ ()
 The prefix ++ operator (++i) advances the iterator to the next item in the map and returns an iterator to the new current item. More...
 
const_iterator operator++ (int)
 The postfix ++ operator (i++) advances the iterator to the next item in the map and returns an iterator to the previously current item. More...
 
const_iteratoroperator+= (int j)
 Advances the iterator by j items. More...
 
const_iterator operator- (int j) const
 Returns an iterator to the item at j positions backward from this iterator. More...
 
const_iteratoroperator-- ()
 The prefix – operator (–i) makes the preceding item current and returns an iterator pointing to the new current item. More...
 
const_iterator operator-- (int)
 The postfix – operator (i–) makes the preceding item current and returns an iterator pointing to the previously current item. More...
 
const_iteratoroperator-= (int j)
 Makes the iterator go back by j items. More...
 
const T * operator-> () const
 Returns a pointer to the current item's value. More...
 
bool operator== (const const_iterator &o) const
 Returns true if other points to the same item as this iterator; otherwise returns false. More...
 
const T & value () const
 Returns the current item's value. More...
 

Private Functions

 operator bool () const
 

Properties

QMapData::Nodei
 

Friends

class iterator
 

Detailed Description

template<class Key, class T>
class QMap< Key, T >::const_iterator

The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.

QMap features both STL-style iterators and Java-style iterators. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.

QMap<Key, T>::const_iterator allows you to iterate over a QMap (or a QMultiMap). If you want to modify the QMap as you iterate over it, you must use QMap::iterator instead. It is generally good practice to use QMap::const_iterator on a non-const QMap as well, unless you need to change the QMap through the iterator. Const iterators are slightly faster, and can improve code readability.

The default QMap::const_iterator constructor creates an uninitialized iterator. You must initialize it using a QMap function like QMap::constBegin(), QMap::constEnd(), or QMap::find() before you can start iterating. Here's a typical loop that prints all the (key, value) pairs stored in a map:

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

Unlike QHash, which stores its items in an arbitrary order, QMap stores its items ordered by key. Items that share the same key (because they were inserted using QMap::insertMulti()) will appear consecutively, from the most recently to the least recently inserted value.

Multiple iterators can be used on the same map. If you add items to the map, existing iterators will remain valid. If you remove items from the map, iterators that point to the removed items will become dangling iterators.

See also
QMap::iterator, QMapIterator

Definition at line 301 of file qmap.h.

Typedefs

◆ difference_type

template<class Key, class T>
QMap< Key, T >::const_iterator::difference_type
Warning
This function is not part of the public interface.

Definition at line 308 of file qmap.h.

◆ iterator_category

template<class Key, class T>
QMap< Key, T >::const_iterator::iterator_category

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

Definition at line 307 of file qmap.h.

◆ pointer

template<class Key, class T>
QMap< Key, T >::const_iterator::pointer
Warning
This function is not part of the public interface.

Definition at line 310 of file qmap.h.

◆ reference

template<class Key, class T>
QMap< Key, T >::const_iterator::reference
Warning
This function is not part of the public interface.

Definition at line 311 of file qmap.h.

◆ value_type

template<class Key, class T>
QMap< Key, T >::const_iterator::value_type
Warning
This function is not part of the public interface.

Definition at line 309 of file qmap.h.

Constructors and Destructors

◆ const_iterator() [1/3]

template<class Key, class T>
QMap< Key, T >::const_iterator::const_iterator ( )
inline

Constructs an uninitialized iterator.

Functions 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
QMap::constBegin() QMap::constEnd()

Definition at line 315 of file qmap.h.

315 : i(0) { }
QMapData::Node * i
Definition: qmap.h:304

◆ const_iterator() [2/3]

template<class Key, class T>
QMap< Key, T >::const_iterator::const_iterator ( QMapData::Node node)
inline
Warning
This function is not part of the public interface.

Definition at line 316 of file qmap.h.

316 : i(node) { }
QMapData::Node * i
Definition: qmap.h:304

◆ const_iterator() [3/3]

template<class Key, class T>
QMap< Key, T >::const_iterator::const_iterator ( const iterator o)
inline

Constructs a copy of other.

Definition at line 320 of file qmap.h.

322  { i = o.i; }
QMapData::Node * i
Definition: qmap.h:304

Functions

◆ key()

template<class Key, class T>
const Key & QMap< Key, T >::const_iterator::key ( ) const
inline

◆ operator bool()

template<class Key, class T>
QMap< Key, T >::const_iterator::operator bool ( ) const
inlineprivate

Definition at line 367 of file qmap.h.

367 { return false; }

◆ operator QMapData::Node *()

template<class Key, class T>
QMap< Key, T >::const_iterator::operator QMapData::Node * ( ) const
inline
Warning
This function is not part of the public interface.

Definition at line 314 of file qmap.h.

314 { return i; }
QMapData::Node * i
Definition: qmap.h:304

◆ operator!=()

template<class Key, class T>
bool QMap< Key, T >::const_iterator::operator!= ( const const_iterator other) const
inline

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

See also
operator==()

Definition at line 332 of file qmap.h.

332 { return i != o.i; }
QMapData::Node * i
Definition: qmap.h:304

◆ operator*()

template<class Key, class T>
const T & QMap< Key, T >::const_iterator::operator* ( ) const
inline

Returns the current item's value.

Same as value().

See also
key()

Definition at line 329 of file qmap.h.

329 { return concrete(i)->value; }
QMapData::Node * i
Definition: qmap.h:304
static Node * concrete(QMapData::Node *node)
Definition: qmap.h:177
T value
Definition: qmap.h:125

◆ operator+()

template<class Key, class T>
QMap::const_iterator QMap< Key, T >::const_iterator::operator+ ( int  j) const
inline

Returns an iterator to the item at j positions forward from this iterator.

(If j is negative, the iterator goes backward.)

This operation can be slow for large j values.

See also
operator-()

Definition at line 352 of file qmap.h.

353  { const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; }
const_iterator()
Constructs an uninitialized iterator.
Definition: qmap.h:315

◆ operator++() [1/2]

template<class Key, class T>
QMap::const_iterator QMap< Key, T >::const_iterator::operator++ ( )
inline

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

Calling this function on QMap::end() leads to undefined results.

See also
operator--()

Definition at line 334 of file qmap.h.

334  {
335  i = i->forward[0];
336  return *this;
337  }
QMapData::Node * i
Definition: qmap.h:304
Node * forward[1]
Definition: qmap.h:65

◆ operator++() [2/2]

template<class Key, class T>
QMap::const_iterator QMap< Key, T >::const_iterator::operator++ ( int  )
inline

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 338 of file qmap.h.

338  {
339  const_iterator r = *this;
340  i = i->forward[0];
341  return r;
342  }
QMapData::Node * i
Definition: qmap.h:304
Node * forward[1]
Definition: qmap.h:65
const_iterator()
Constructs an uninitialized iterator.
Definition: qmap.h:315

◆ operator+=()

template<class Key, class T>
QMap::const_iterator & QMap< Key, T >::const_iterator::operator+= ( int  j)
inline

Advances the iterator by j items.

(If j is negative, the iterator goes backward.)

This operation can be slow for large j values.

See also
operator-=(), operator+()

Definition at line 355 of file qmap.h.

355 { return *this = *this + j; }

◆ operator-()

template<class Key, class T>
QMap::const_iterator QMap< Key, T >::const_iterator::operator- ( int  j) const
inline

Returns an iterator to the item at j positions backward from this iterator.

(If j is negative, the iterator goes forward.)

This operation can be slow for large j values.

See also
operator+()

Definition at line 354 of file qmap.h.

354 { return operator+(-j); }
const_iterator operator+(int j) const
Returns an iterator to the item at j positions forward from this iterator.
Definition: qmap.h:352

◆ operator--() [1/2]

template<class Key, class T>
QMap::const_iterator & QMap< Key, T >::const_iterator::operator-- ( )
inline

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

Calling this function on QMap::begin() leads to undefined results.

See also
operator++()

Definition at line 343 of file qmap.h.

343  {
344  i = i->backward;
345  return *this;
346  }
QMapData::Node * i
Definition: qmap.h:304
Node * backward
Definition: qmap.h:64

◆ operator--() [2/2]

template<class Key, class T>
QMap::const_iterator QMap< Key, T >::const_iterator::operator-- ( int  )
inline

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 347 of file qmap.h.

347  {
348  const_iterator r = *this;
349  i = i->backward;
350  return r;
351  }
QMapData::Node * i
Definition: qmap.h:304
const_iterator()
Constructs an uninitialized iterator.
Definition: qmap.h:315
Node * backward
Definition: qmap.h:64

◆ operator-=()

template<class Key, class T>
QMap::const_iterator & QMap< Key, T >::const_iterator::operator-= ( int  j)
inline

Makes the iterator go back by j items.

(If j is negative, the iterator goes forward.)

This operation can be slow for large j values.

See also
operator+=(), operator-()

Definition at line 356 of file qmap.h.

356 { return *this = *this - j; }

◆ operator->()

template<class Key, class T>
const T * QMap< Key, T >::const_iterator::operator-> ( ) const
inline

Returns a pointer to the current item's value.

See also
value()

Definition at line 330 of file qmap.h.

330 { return &concrete(i)->value; }
QMapData::Node * i
Definition: qmap.h:304
static Node * concrete(QMapData::Node *node)
Definition: qmap.h:177
T value
Definition: qmap.h:125

◆ operator==()

template<class Key, class T>
bool QMap< Key, T >::const_iterator::operator== ( const const_iterator other) const
inline

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

See also
operator!=()

Definition at line 331 of file qmap.h.

331 { return i == o.i; }
QMapData::Node * i
Definition: qmap.h:304

◆ value()

template<class Key, class T>
const T & QMap< Key, T >::const_iterator::value ( ) const
inline

Friends and Related Functions

◆ iterator

template<class Key, class T>
friend class iterator
friend

Definition at line 303 of file qmap.h.

Properties

◆ i

template<class Key, class T>
QMapData::Node* QMap< Key, T >::const_iterator::i
private

The documentation for this class was generated from the following files: