how to compare QStringList to std::string vector
-
wrote on 16 Sept 2018, 10:55 last edited by
how to compare a std::vector<string> to a QStringList
-
how to compare a std::vector<string> to a QStringList
wrote on 16 Sept 2018, 11:12 last edited by -
wrote on 16 Sept 2018, 11:19 last edited by
Though you can convert the
QStringList
tostd::vector<QString>
by usingQStringList::toVector()
andQVector::toStdVector
.
But there seems to be no single function to compare astd::string
to aQString
.
So you need to write one such function. -
Hi,
What exact comparison do you have in mind ? That they have the same content in the same order ? Or that they have all values in common ?
-
Hi,
What exact comparison do you have in mind ? That they have the same content in the same order ? Or that they have all values in common ?
wrote on 17 Sept 2018, 06:39 last edited byalll values in common
-
alll values in common
You cannot directly compare
QString
andstd::string
as the former is Unicode UTF-16 and the latter may have one of the 8 bit encodings.So you need to convert one of them anyway.
-
from what the others already said, you could do something like this:
bool compareQStdString(const QString &str1, const std::string &str2){ return (str1 == QString::fromStdString(str2)); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QList<QString> qstrings({"a","b","c","d","e","f","g"}); std::vector<std::string>strings({"a","b","b","d","d","f","f"}); for(int i(0); i < qstrings.size() && i < strings.size(); i++){ qDebug() << "Strings are at index"<< i << "are equal:" << compareQStdString(qstrings.at(i), strings.at(i)); } return a.exec(); } /*Result: Strings are at index 0 are equal: true Strings are at index 1 are equal: true Strings are at index 2 are equal: false Strings are at index 3 are equal: true Strings are at index 4 are equal: false Strings are at index 5 are equal: true Strings are at index 6 are equal: false */
It's probably not the fastest or most elegant way to do it, but it works at least.
1/7