libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
Quotient::asKeyValueRange< T > Class Template Reference

An adaptor for Qt (hash-)maps to make them iterable in STL style. More...

#include <util.h>

Public Member Functions

 asKeyValueRange (T data)
 
auto begin ()
 
auto end ()
 
auto begin () const
 
auto end () const
 

Detailed Description

template<typename T>
class Quotient::asKeyValueRange< T >

An adaptor for Qt (hash-)maps to make them iterable in STL style.

QMap/QHash container iterators returned by begin() and end() dereference to values, unlike STL where similar iterators dereference to key-value pairs. It is a problem in range-for if you want to also access map keys. This adaptor allows to use range-for syntax with access to both keys and values in QMap/QHash containers. Just use for (auto&& [key, value] : asKeyValueRange(myMap) instead of for (auto&& value : myMap).

Note
When an rvalue is passed as the constructor argument, asKeyValueRange shallow-copies the map object to ensure its lifetime is maintained throughout the loop; with lvalues, no copying occurs, assuming that the map object outlives the range-for loop

Definition at line 184 of file util.h.

Constructor & Destructor Documentation

◆ asKeyValueRange()

template<typename T >
Quotient::asKeyValueRange< T >::asKeyValueRange ( T  data)
inlineexplicit

Definition at line 187 of file util.h.

Member Function Documentation

◆ begin() [1/2]

template<typename T >
auto Quotient::asKeyValueRange< T >::begin ( )
inline

Definition at line 191 of file util.h.

◆ begin() [2/2]

template<typename T >
auto Quotient::asKeyValueRange< T >::begin ( ) const
inline

Definition at line 199 of file util.h.

◆ end() [1/2]

template<typename T >
auto Quotient::asKeyValueRange< T >::end ( )
inline

Definition at line 195 of file util.h.

◆ end() [2/2]

template<typename T >
auto Quotient::asKeyValueRange< T >::end ( ) const
inline

Definition at line 200 of file util.h.


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