Convert vector<string> to QVector<QString>

Viewed 567

What is the most basic way to convert a std::vector<std::string> to QVector<QString>?

std::vector<std::string> plain = {"apple", "orange", "banana", "mango", "blueberry"};
QVector<QString> qt;
1 Answers

Not a one-line but a two-liner...

QVector<QString> qt;
std::transform(plain.begin(), plain.end(), std::back_inserter(qt), [](const std::string &v){ return QString::fromStdString(v); });

And as addendum to taiwan12's answer and mine as well I would suggest you should use qt.reserve(plain.size()); before adding the strings. Which would make my answer three-liner. What a shame!

Regarding performance, my measurements show that both solutions (mine and taiwan12's) work equally fast.

Related