QStringList与迭代-QT4

#include <QStringList>
#include <QDebug>
#include <cassert>

int main() {

    QString winter = "December, January, February";
    QString spring = "March, April, May";
    QString summer = "June, July, August";
    QString fall = "September, October, November";

    QStringList list;    // QStringList重载了许多函数和操作符
    list << winter;        /* append operator 1 */
    list += spring;        /* append operator 2 */
    list.append(summer);   /* append member function */
    list << fall;

    qDebug() << "The Spring months are: " << list[1] ;

    QString allmonths = list.join(", "); //将“,”加到QStringList
    /* from list to string - join with a ", " delimiter */
    qDebug() << allmonths;

    QStringList list2 = allmonths.split(", ");  //按照“,”将QStringList分割成QString
    /* split is the opposite of join. Each month will have its own element. */

    assert(list2.size() == 12); /* assertions abort the program  此时list2有12个元素
    if the condition is not satisfied. */

     for (QStringList::iterator it = list.begin(); it != list.end(); ++it) { 

            /* C++ STL-style iteration */

            QString current = *it;   /* pointer-style dereference */
            qDebug() << "[[" << current << "]]";
        }
    }
    return 0;
}

你可能感兴趣的:(QStringList与迭代-QT4)