【问题标题】:How to use java-style iterators instead stl-style in the Qt?如何在 Qt 中使用 java 风格的迭代器而不是 stl 风格?
【发布时间】:2014-12-25 20:29:20
【问题描述】:

例如:

QList<QMap<QString,QString> > list

QList<QMap<QString,QString> >::iterator i;
    for (i = list.begin(); i != list.end(); ++i)
    {
        QMap<QString,QString>::iterator j;
        for (j = i->begin(); j != i->end(); ++j)
            qDebug() << j.key() << ": " << j.value() << endl;
    }

所以这种 stl 风格是有效的。但我无法重写为 java 风格:

   QListIterator<QMap<QString,QString> > i(list);
    while (i.hasNext())
    {
        QMapIterator<QString,QString> j(i); //error 
        while (j.hasNext()) {
            j.next();
            qDebug() << j.key() << ": " << j.value() << endl;
        }
    }

这里我得到错误 - cannot convert parameter 1 from QListIterator&lt;T&gt;' to 'const QMap&lt;Key,T&gt;

【问题讨论】:

  • 我的回答是否为您解决了问题?

标签: c++ qt iterator qtcore qmap


【解决方案1】:

使用java风格的迭代器时需要获取下一项如下:

 QMapIterator<QString,QString> j(i.next()); //no error

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2021-03-28
    相关资源
    最近更新 更多