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) {  spring

            /* C++ STL-style iteration */ app

            QString current = *it;   /* pointer-style dereference */
            qDebug() << "[[" << current << "]]";
        }
    }
    return 0;
}
相關文章
相關標籤/搜索