diff options
author | Michael Winkelmann <michael.winkelmann@qt.io> | 2017-08-03 15:43:26 +0200 |
---|---|---|
committer | Topi Reiniƶ <topi.reinio@qt.io> | 2017-11-07 09:42:34 +0000 |
commit | ab9f4d5db6a4e183b9a26366e659ba642dedcbdd (patch) | |
tree | 2661ab6baacd6ec545449619a749d8a9f4e43c54 /examples/qtconcurrent/wordcount | |
parent | d4b3ce9a287b81eb9d7c6750ccca89e362343261 (diff) |
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 <edward.welbourne@qt.io>
Diffstat (limited to 'examples/qtconcurrent/wordcount')
-rw-r--r-- | examples/qtconcurrent/wordcount/main.cpp | 27 |
1 files changed, 15 insertions, 12 deletions
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<QString, int> 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); } |