tez
tez

Reputation: 5290

QVector object construction with iterators

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??

Upvotes: 3

Views: 917

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 476970

As a work-around, you could use fromStdVector:

auto qv = QVector<double>::fromStdVector(std::vector<double>(
                         vec_old.begin() + 100, vec_old.end()));

Upvotes: 5

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.

Upvotes: 4

Related Questions