QMap 迭代器QMapIterator、QMutableMapIterator的使用

118 篇文章 8 订阅

QMap迭代器QMapIterator、QMutableMapIterator的使用

QMap迭代器——QMapIterator :官方文件的第三方译文

QMutableMapIterator Class :官方文件的第三方译文

   https://runebook.dev/zh/docs/qt/qmutablemapiterator

-------------------------------------------------------------------------

要点:

QMapIterator:只读,无 setValue() 方法

QMutableMapIterator:可以修改,有 setValue()方法

=======================================

QMap迭代器——QMapIterator :官方译文

QMapIterator Class

QMap迭代器,看的时候,顺便翻译了。

QMapIterator 类提供了一个Java风格的常量迭代器。

。。。。。。省略。。。。。。。

详细描述

QMapIterator 类为QMap和QMultiMap,提供了一个Java风格的常量迭代器。

QMap有Java风格迭代器和STL风格迭代器。Java风格迭代器比STL风格迭代器更高级而且更好用,但其效率不及STL风格迭代器。

使用QMapIterator<Key, T>对一个QMap (或一个 QMultiMap)进行迭代。当你需要在迭代时能够修改map,则需要使用QMutableMapIterator

QMutableMapIterator 迭代器的构造函数以一个QMap作为参数。当构造完成后,迭代器位于map的最开始(即表的第一项之前)。下面的代码描述了怎样按顺序迭代所有元素。

    QMap<int, QWidget *> map;...
    QMapIterator<int, QWidget *> i(map);
    while(i.hasNext()) {
    I.next();
    qDebug() << i.key() << “:” << i.value();
    }

next()函数返回map中的下一个项。Key()和value()函数返回上一个项的键(key)和数值(value)

不同于STL风格迭代器,Java风格迭代器地向项目之间而不是指向map中的项。第一次调用next(),迭代器指向第一项和第二项之间,返回第一项;第二次调用next(),迭代器指向第二项和第三项之间,返回第二项,以此类推。

下面是如何反向迭代访问map中的元素项目。

    QMapIterator<int, QWidget *> i(map);
    i.toBack();
    while(i.hasPrevious()) {
    I.previous();
    qDebug() << i.key() << “:” << i.value();
    }


如果要遍历表,可在循环中使用findNext() or findPrevious(),例如:

    QMapIterator<int, QWidget *> i(map);
    i.toBack();
    while(i.hasPrevious()) {
    I.previous();
    qDebug() << i.key() << “:” << i.value();
    }


在相同的map上可以使用多个迭代器。如果在使用一个QMapIterator 时,map被修改了,QMapIterator仍然按照原始的表继续进行迭代被修改的项将被忽略。

See also QMutableMapIterator and QMap::const_iterator.
成员函数详解
QMapIterator::​QMapIterator(const QMap<Key, T> & map)

Constructs an iterator for traversing map. The iterator is set to be at the front of the map (before the first item).

See also operator=().
bool QMapIterator::​findNext(const T & value)

Searches for value starting from the current iterator position forward. Returns true if a (key, value) pair with value value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just after the matching item; otherwise, the iterator is positioned at the back of the container.

See also findPrevious().
bool QMapIterator::​findPrevious(const T & value)

Searches for value starting from the current iterator position backward. Returns true if a (key, value) pair with value value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just before the matching item; otherwise, the iterator is positioned at the front of the container.

See also findNext().
bool QMapIterator::​hasNext() const

Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returnsfalse.

See also hasPrevious() and next().
bool QMapIterator::​hasPrevious() const

Returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returnsfalse.

See also hasNext() and previous().
const Key & QMapIterator::​key() const

Returns the key of the last item that was jumped over using one of the traversal functions (next(), previous(), findNext(),findPrevious()).

After a call to next() or findNext(), key() is equivalent to peekPrevious().key(). After a call to previous() or findPrevious(), key() is equivalent to peekNext().key().

See also value().
Item QMapIterator::​next()

Returns the next item and advances the iterator by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also hasNext(), peekNext(), and previous().
Item QMapIterator::​peekNext() const

Returns the next item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also hasNext(), next(), and peekPrevious().
Item QMapIterator::​peekPrevious() const

Returns the previous item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the front of the container leads to undefined results.

See also hasPrevious(), previous(), and peekNext().
Item QMapIterator::​previous()

Returns the previous item and moves the iterator back by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the front of the container leads to undefined results.

See also hasPrevious(), peekPrevious(), and next().
void QMapIterator::​toBack()

Moves the iterator to the back of the container (after the last item).

See also toFront() and previous().
void QMapIterator::​toFront()

Moves the iterator to the front of the container (before the first item).

See also toBack() and next().
const T & QMapIterator::​value() const

Returns the value of the last item that was jumped over using one of the traversal functions (next(), previous(), findNext(),findPrevious()).

After a call to next() or findNext(), value() is equivalent to peekPrevious().value(). After a call to previous() or findPrevious(), value() is equivalent to peekNext().value().

See also key().
QMapIterator & QMapIterator::​operator=(const QMap<Key, T> & map)

Makes the iterator operate on map. The iterator is set to be at the front of the map (before the first item).

See also toFront() and toBack().
————————————————
版权声明:本文为CSDN博主「紫梧桐」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/waangyan/article/details/68485176

QMap迭代器QMapIterator、QMutableMapIterator的使用

1.使用示例

#include <QCoreApplication>
#include <QMapIterator>
#include <QMutableMapIterator>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMap<QString, QString> map;
    map.insert("Paris", "France");
    map.insert("Guatemala City", "Guatemala");
    map.insert("Mexico City", "Mexico");
    map.insert("Moscow", "Russia");

  

    QMapIterator<QString,QString> i(map);
    while(i.hasNext()) {
        i.next();
        qDebug() << i.key() << " : " << i.value();
    }
    if(i.findPrevious("Mexico")) qDebug() << "find 'Mexico'";  // 向前查找键的值

   

    QMutableMapIterator<QString, QString> j(map);
    while (j.hasNext()) {
        if (j.next().key().endsWith("City")) // endsWith()是QString类的函数
            j.remove();                      // 删除含有“City”结尾的键的项目
    }
    while(j.hasPrevious()) {
        j.previous();          // 现在的键值对为(paris,France),(Moscow,Russia)
        qDebug() << j.key() << " : " << j.value();
    }
    
    return a.exec();
}

 

 ————————————————
版权声明:本文为CSDN博主「超级大洋葱806」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014779536/article/details/111149886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值