Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I map lists in Qt?

Tags:

c++

qt

qlist

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());
}
like image 219
Internet man Avatar asked Dec 17 '22 22:12

Internet man


1 Answers

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));
like image 125
Mike Seymour Avatar answered Dec 19 '22 12:12

Mike Seymour