Casting QVector<int> to QVector<double>
-
Hi and welcome to devnet,
Why do you need to do that for ?
-
@dovahin said in Casting QVector<int> to QVector<double>:
I know I could iterate through both of them, but looking for something better
What @SGaist wrote. Also there's nothing better.
QVector<int>
andQVector<double>
are two different and distinct classes, you can't just cast from one to the other. -
@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 :)