Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Casting QVector<int> to QVector<double>
Is there an elegant way of casting QVector<int> to QVector<double> ? I know I could iterate through both of them, but looking for something better
Hi and welcome to devnet,
Why do you need to do that for ?
I know I could iterate through both of them, but looking for something better
What @SGaist wrote. Also there's nothing better.
QVector<double>are two different and distinct classes, you can't just cast from one to the other.
@SGaist because I have QVector<int> and I need to pass it to qwt_plot_currve function setSamples which takes QVector<double>
@dovahin Either you use QVector<double> from the beginning, then there is no need to cast anything. Or you will have to iterate over the element of your QVector<int> and add each element to the QVector<double> which isn't efficient.
As @kshegunov said both classes are different and you can't simply cast one to the other.
you could try to initialize your QVectpr<double>(myOtherVector.size()) and than try direct data manipulation via T
*QVector::data()but you can not simply memcopy the data over. It won't be the same size and is not reinterpret_cast compatible.
The propose idea to use QVector<double> in the first place is the fastest one. But if you really need to convert such a vector:
QVector<int> in; QVector<double> out; out.reserve(in.size()); std::copy(in.cbegin(), in.cend(), std::back_inserter(out));
@Christian-Ehrlicher thanks, thats solve my problem :)