QSet docs: don't use std::find for lookups

That code would normally call for QSet::find instead
(Uniform Container Find cannot come soon enough).

Since the code is showcasing a STL algorithm usage,
port it to std::find_if to showcase a real use case.

As a drive-by: fix the usage of endl with std::cout.
(Ok, it's just a variable called "cout", and I'd argue
that in example code "cout" is not the name of a
QTextStream).

Change-Id: I8686178b33c31552eb4d909a4089453d60994b79
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Giuseppe D'Angelo 2019-06-30 12:02:24 +02:00
parent 1bddb4ad7d
commit 656117100b
1 changed files with 6 additions and 4 deletions

View File

@ -131,9 +131,10 @@ while (i != set.end()) {
//! [10]
QSet<QString> set;
...
QSet<QString>::iterator it = std::find(set.begin(), set.end(), "Jeanette");
const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; };
QSet<QString>::iterator it = std::find_if(set.begin(), set.end(), predicate);
if (it != set.end())
cout << "Found Jeanette" << Qt::endl;
cout << "Found Jeanette" << endl;
//! [10]
@ -150,9 +151,10 @@ for (i = set.begin(); i != set.end(); ++i)
//! [12]
QSet<QString> set;
...
QSet<QString>::iterator it = std::find(set.begin(), set.end(), "Jeanette");
const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; };
QSet<QString>::const_iterator it = std::find_if(set.cbegin(), set.cend(), predicate);
if (it != set.constEnd())
cout << "Found Jeanette" << Qt::endl;
cout << "Found Jeanette" << endl;
//! [12]