user16554286
user16554286

Reputation:

Convert vector<string> to QVector<QString>

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;

Upvotes: 1

Views: 2151

Answers (1)

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.

Upvotes: 2

Related Questions