Can I display lists in Qt?

This is already pretty concise, but it would be great if I could list a la Ruby. Let's say I have QStringListmyStringList that contains things like "12.3", "-213.0", "9.24". I want to simply match all this with toDoubleno repetition. 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());
}
+3
source share
2 answers

As far as I can tell, QT containers have an interface compatible with standard containers, so you should be able to use standard algorithms. In this case, something like

std::transform(myStringList.begin(), 
               myStringList.end(), 
               std::back_inserter(myDoubleList),
               std::mem_fun(&QString::toDouble));
+8
source

toDouble . :

class TransformIterator : public std::iterator<input_iterator_tag, double, ptrdiff_t, double*, double&>
{
  StringList::const_iterator baseIter;
public:
  TransformIterator(StringList::const_iterator baseIter) : baseIter(baseIter) { }
  TransformIterator operator++() { ++baseIter; return *this; }
  double operator*() const { return baseIter->toDouble(); }
};

QList<double> myDoubleList(TransformIterator(myStringList.begin()),
                           TransformIterator(myStringList.end())); 
0

Source: https://habr.com/ru/post/1757812/


All Articles