Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVector object construction with iterators

Tags:

c++

qt

Is it not possible to construct a new QVector object from iterators like C++ vectors??

QVector<double> new_vec(vec_old.begin()+100,vec_old.end())

I'm getting errors when I'm trying to do something like this.Also what is the best way to construct a new QVector object from a part of other QVector??

like image 256
tez Avatar asked Jun 27 '26 08:06

tez


2 Answers

As a work-around, you could use fromStdVector:

auto qv = QVector<double>::fromStdVector(std::vector<double>(
                         vec_old.begin() + 100, vec_old.end()));
like image 179
Kerrek SB Avatar answered Jun 28 '26 22:06

Kerrek SB


According to Qt documentation, this is not possible.

QVector Class Reference

Answering your second question, to create a QVector from a part of other QVector, I believe the following is one of the best options:

QVector<double> new_vec(vec_old.size-100);
double* dt = vec_old.constData;
dt += 100; // some pointer arithmetic.
new_vec.fill((*dt), vec_old.size-100);

This will only copy data starting at some specified position until the end, just like you wrote.

like image 36
Vinícius Gobbo A. de Oliveira Avatar answered Jun 28 '26 21:06

Vinícius Gobbo A. de Oliveira