QAssocMutableIterator Class
template <typename C, typename K, typename V, bool Bidirectional> class QAssocMutableIterator
The QAssocMutableIterator class provides the Java-style non-const iterator behind QMutableMapIterator, QMutableMultiMapIterator and QMutableHashIterator. More…
| Header | #include <QtCore/qiterator.h> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QAssocMutableIterator(C &container) | |
| void | toFront() |
| void | toBack() |
| bool | hasNext() const |
| It | next() |
| const K & | key() const |
| V & | value() const |
| void | setValue(const V &v) |
| void | remove() |
| It | peekNext() const |
| bool | findNext(const V &v) |
| bool | hasPrevious() const |
| It | previous() |
| It | peekPrevious() const |
| bool | findPrevious(const V &v) |
Detailed Description
QAssocMutableIterator<C, K, V, Bidirectional> iterates over an associative container C and can modify it. Like QAssocConstIterator it points between items; key(), value(), setValue() and remove() act on the item that was last jumped over. Use the aliases QMutableMapIterator, QMutableMultiMapIterator and QMutableHashIterator rather than naming this class.
QMap<QString, int> map = {{"a", 1}, {"b", 2}};
QMutableMapIterator<QString, int> it(map);
while (it.hasNext()) {
it.next();
if (it.value() < 2)
it.remove();
}
When Bidirectional is false, as for QMutableHashIterator, the functions that move backward (hasPrevious(), previous(), peekPrevious() and findPrevious()) are not available.
See also QAssocConstIterator and QMutableListIterator.
Differences from Qt
next(), peekNext(), previous() and peekPrevious() return an iterator of the container rather than a key-value item. There is no assignment from a container.
Member Function Documentation
QAssocMutableIterator::QAssocMutableIterator(C &container)
Constructs an iterator for traversing container. The iterator is set to be at the front of the container, before the first item.
See also toFront().
bool QAssocMutableIterator::findNext(const V &v)
Searches for the value v forward from the iterator position. Returns true if an item with that value is found, leaving the iterator just after it so that key() and value() describe it; otherwise returns false and leaves the iterator at the back.
See also findPrevious().
template <bool B = Bidirectional, typename = std::enable_if_t<B>>bool QAssocMutableIterator::findPrevious(const V &v)
Searches for the value v backward from the iterator position. Returns true if an item with that value is found, leaving the iterator just before it so that key() and value() describe it; otherwise returns false and leaves the iterator at the front. Only available when the container is bidirectional.
See also findNext().
bool QAssocMutableIterator::hasNext() const
Returns true if there is at least one item ahead of the iterator; otherwise returns false.
See also hasPrevious() and next().
template <bool B = Bidirectional, typename = std::enable_if_t<B>>bool QAssocMutableIterator::hasPrevious() const
Returns true if there is at least one item behind the iterator; otherwise returns false. Only available when the container is bidirectional.
See also hasNext() and previous().
const K & QAssocMutableIterator::key() const
Returns the key of the item that was last jumped over by next() or previous().
See also value().
It QAssocMutableIterator::next()
Returns the next item and advances the iterator by one position. The key and value of the returned item are then available from key() and value(). Calling this function when the iterator is at the back is undefined.
See also hasNext(), peekNext() and previous().
It QAssocMutableIterator::peekNext() const
Returns the next item without moving the iterator. Calling this function when the iterator is at the back is undefined.
See also hasNext(), next() and peekPrevious().
template <bool B = Bidirectional, typename = std::enable_if_t<B>>It QAssocMutableIterator::peekPrevious() const
Returns the previous item without moving the iterator. Only available when the container is bidirectional. Calling this function when the iterator is at the front is undefined.
See also hasPrevious(), previous() and peekNext().
template <bool B = Bidirectional, typename = std::enable_if_t<B>>It QAssocMutableIterator::previous()
Returns the previous item and moves the iterator back by one position. Only available when the container is bidirectional. Calling this function when the iterator is at the front is undefined.
See also hasPrevious(), peekPrevious() and next().
void QAssocMutableIterator::remove()
Removes the item that was last jumped over by next() or previous(). The iterator then sits at the item that followed the removed one. Does nothing if there is no such item.
See also setValue().
void QAssocMutableIterator::setValue(const V &v)
Replaces the value of the item that was last jumped over with v. Does nothing if there is no such item.
See also value() and remove().
void QAssocMutableIterator::toBack()
Moves the iterator to the back of the container, after the last item.
See also toFront() and previous().
void QAssocMutableIterator::toFront()
Moves the iterator to the front of the container, before the first item.
V & QAssocMutableIterator::value() const
Returns a reference to the value of the item that was last jumped over by next() or previous().
See also key() and setValue().