How to convert const QVector<quint32> to vector<int>.
-
Once I'm sure I need the conversion, I would use std::transform with a lambda.
-
Apart from the ususal disclaimer about converting unsigned to siged, you can use
std::vector<int> l_sensorAddressList( reinterpret_cast<const int*>(p_sensorAddressList.constData()) , reinterpret_cast<const int*>(p_sensorAddressList.constData()+p_sensorAddressList.size()) );
-
While we're at it:
std::copy(in.begin(), in.end(), std::back_inserter(out));
-
Hi,
One even simpler version:
std::vector<int> l_sensorAddressList(std::begin(p_sensorAddressList), std::end(p_sensorAddressList));
or
std::vector<int> l_sensorAddressList(p_sensorAddressList.begin(), p_sensorAddressList.end());
-
Just to clarify: @JohanSolo , @Christian-Ehrlicher and @SGaist 's solutions are correct. My solution should be faster as it's a simple
memcpy
but the other's might be safer (I don't really see how though) -
@Christian-Ehrlicher Are iterators faster than indexing ([index]) ?
-
@JonB said in How to convert const QVector<quint32> to vector<int>.:
which I assumed is 64-bit now?
int is 32 bit, on every platform Qt is running on :)
-
@Christian-Ehrlicher
Wot? Really?? I thought it was 64 on 64.... -
@JonB said in How to convert const QVector<quint32> to vector<int>.:
Wot? Really?? I thought it was 64 on 64....
Pointer size is increased to 64 but integer size remains 32 on today's common 64-bit desktop architectures.
sizeof(qintptr) == sizeof(void*) != sizeof(int)
-
@JKSH said in How to convert const QVector<quint32> to vector<int>.:
sizeof(qintptr) == sizeof(void*) != sizeof(int)
wow, I totally assumed differently,
I should make it habit to use the explicitly size defined qintXX/quintXX in the future... -
@J-Hilk said in How to convert const QVector<quint32> to vector<int>.:
I should make it habit to use the explicitly size defined qintXX/quintXX in the future...
Good idea!
-
Pointer size is increased to 64 but integer size remains 32 on today's common 64-bit desktop architectures.
Yes, now I vaguely remember reading this, I am glad @J-Hilk shared my mis-assumption!
This is what comes of my being forced to use Python for Qt or JavaScript over so many years now. Ignorance of underlying type sizes! It has been a long time since
sizeof(int) != sizeof(long)
andsizeof(int) != sizeof(void*)
, probably not since the initial move from 16-bit to 32-bit architecture! :)