Question:
How to apply STL algorithms in Qt, for example QStringList
and set_difference
, for example? If it is possible of course.
Answer:
All container containers have begin()
and end()
methods (as well as cbegin()
etc.) that return STL-compatible iterators; they also have QList
, which QStringList
. They should be passed to STL functions of generalized algorithms.
QSet<QString> strongWords{"криворучкие", "дураки", /*...*/};
//....
QString userOpinion = getUserOpinion();
QStringList userOpinionWords = qSort(userOpinion.split(QRegExp("\\s+")'));
QStrinList inapropriateWords;
std::set_difference(userOpinionWords.cbegin(), userOpinionWords.cend()
strongWords.cbegin(), strongWords.cend(),
std::inserter(inapropriateWords, inapropriateWords.begin()));
if (!inapropriateWords.isEmpty()) {
QMessageBox::info (QString ("Ваш отзыв очень важен для нас, но следующие слова неприемлимы: ").append(inapropriateWords.join(", ")));
throw std::runtime_error ("Пользователь был груб, поэтому мы бросим исключение и не будем его ловить.");
}
You also need to remember that, at its core, std::set_difference()
accepts ordered containers as input …