From ab9f4d5db6a4e183b9a26366e659ba642dedcbdd Mon Sep 17 00:00:00 2001 From: Michael Winkelmann Date: Thu, 3 Aug 2017 15:43:26 +0200 Subject: Revamp QtConcurrent examples to C++11 I updated signals and slots and for each loops to the new syntax and replaced most free functions with std::function. Task-number: QTBUG-60641 Change-Id: I7693f81f71c7f53fcbe83189a0de2fb76ddf99a8 Reviewed-by: Edward Welbourne --- examples/qtconcurrent/wordcount/main.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'examples/qtconcurrent/wordcount') diff --git a/examples/qtconcurrent/wordcount/main.cpp b/examples/qtconcurrent/wordcount/main.cpp index a5f8909f34..ff7ea24ee7 100644 --- a/examples/qtconcurrent/wordcount/main.cpp +++ b/examples/qtconcurrent/wordcount/main.cpp @@ -65,15 +65,17 @@ using namespace QtConcurrent; /* Utility function that recursivily searches for files. */ -QStringList findFiles(const QString &startDir, QStringList filters) +QStringList findFiles(const QString &startDir, const QStringList &filters) { QStringList names; QDir dir(startDir); - foreach (QString file, dir.entryList(filters, QDir::Files)) + const auto files = dir.entryList(filters, QDir::Files); + for (const QString &file : files) names += startDir + '/' + file; - foreach (QString subdir, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot)) + const auto subdirs = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot); + for (const QString &subdir : subdirs) names += findFiles(startDir + '/' + subdir, filters); return names; } @@ -83,17 +85,18 @@ typedef QMap WordCount; /* Single threaded word counter function. */ -WordCount singleThreadedWordCount(QStringList files) +WordCount singleThreadedWordCount(const QStringList &files) { WordCount wordCount; - foreach (QString file, files) { + for (const QString &file : files) { QFile f(file); f.open(QIODevice::ReadOnly); QTextStream textStream(&f); - while (textStream.atEnd() == false) - foreach (const QString &word, textStream.readLine().split(' ')) + while (!textStream.atEnd()) { + const auto words = textStream.readLine().split(' '); + for (const QString &word : words) wordCount[word] += 1; - + } } return wordCount; } @@ -109,9 +112,11 @@ WordCount countWords(const QString &file) QTextStream textStream(&f); WordCount wordCount; - while (textStream.atEnd() == false) - foreach (const QString &word, textStream.readLine().split(' ')) + while (!textStream.atEnd()) { + const auto words = textStream.readLine().split(' '); + for (const QString &word : words) wordCount[word] += 1; + } return wordCount; } @@ -137,8 +142,6 @@ int main(int argc, char** argv) qDebug() << "warmup"; { - QTime time; - time.start(); WordCount total = singleThreadedWordCount(files); } -- cgit v1.2.3