This is already fairly concise, but it would be awesome if I could map the list a la Ruby. Say I have a QStringList
myStringList which contains things like "12.3", "-213.0", "9.24". I want to simply map the whole thing using toDouble
without having to iterate. Does Qt have a method for this?
// i.e. I would love a one-liner for the following
// NB QT provices foreach
QList<double> myDoubleList;
foreach(QString s, myStringList) {
myDoubleList.append(s.toDouble());
}
As far as I can tell, QT's containers have an interface compatible with the Standard containers, so you should be able to use Standard algorithms on them. In this case, something like
std::transform(myStringList.begin(),
myStringList.end(),
std::back_inserter(myDoubleList),
std::mem_fun(&QString::toDouble));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With