QVector has no assign method?
-
Does QVector not have the ability to set the value at any index e.g.
int index = rand(); myvector[index] = rand();
There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.
BTW I see that Qt5 has no QArray. Not sure if there formally was one.
-
Does QVector not have the ability to set the value at any index e.g.
int index = rand(); myvector[index] = rand();
There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.
BTW I see that Qt5 has no QArray. Not sure if there formally was one.
@Publicnamer said in QVector has no assign method?:
There are many cases where it's important to not bother with resizing
With QVector you have to. index must be: 0 <= index < myvector.size()
https://doc.qt.io/qt-5/qvector.html#operator-5b-5d
QVector is basically a resizeable array. -
Does QVector not have the ability to set the value at any index e.g.
int index = rand(); myvector[index] = rand();
There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.
BTW I see that Qt5 has no QArray. Not sure if there formally was one.
@Publicnamer said in QVector has no assign method?:
And yet a hash isn't good enough.
I don't understand this statement.
If you want no array/vector resizing, and no pre-allocated maximum elements, and you don't want to use a list (do you?), it's a
QHash
or aQMap
. -
@Publicnamer said in QVector has no assign method?:
There are many cases where it's important to not bother with resizing
With QVector you have to. index must be: 0 <= index < myvector.size()
https://doc.qt.io/qt-5/qvector.html#operator-5b-5d
QVector is basically a resizeable array.@jsulm said in QVector has no assign method?:
QVector is basically a resizeable array.
Isn't there an automatically resized array?
-
@jsulm said in QVector has no assign method?:
QVector is basically a resizeable array.
Isn't there an automatically resized array?
@Publicnamer said in QVector has no assign method?:
Isn't there an automatically resized array?
Such a thing would be extremely dangerous as it would simply resize if the programmer uses an invalid index instead of crashing the application with "out of bounds" exception.
Why do you need such a thing actually?! What is the problem to properly resize the QVector? -
@jsulm said in QVector has no assign method?:
QVector is basically a resizeable array.
Isn't there an automatically resized array?
@Publicnamer said in QVector has no assign method?:
Isn't there an automatically resized array?
As @jsulm says.
However if you really want this then (presumably, untested) you can derive fromQVector
and override the[]
operator?EDIT
I don't think you can override the[]
operator after all?
In which case it may not be as "elegant" as you might wish, but you do realize that before going[index]
you can insertif (index >= qVector.size()) qVector.resize(index);
Up to you whether you want to do this where you are using
qVector[index]
only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology). -
@Publicnamer said in QVector has no assign method?:
Isn't there an automatically resized array?
As @jsulm says.
However if you really want this then (presumably, untested) you can derive fromQVector
and override the[]
operator?EDIT
I don't think you can override the[]
operator after all?
In which case it may not be as "elegant" as you might wish, but you do realize that before going[index]
you can insertif (index >= qVector.size()) qVector.resize(index);
Up to you whether you want to do this where you are using
qVector[index]
only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology).@JonB said in QVector has no assign method?:
I don't think you can override the [] operator after all?
Why not?
-
@Publicnamer said in QVector has no assign method?:
Isn't there an automatically resized array?
As @jsulm says.
However if you really want this then (presumably, untested) you can derive fromQVector
and override the[]
operator?EDIT
I don't think you can override the[]
operator after all?
In which case it may not be as "elegant" as you might wish, but you do realize that before going[index]
you can insertif (index >= qVector.size()) qVector.resize(index);
Up to you whether you want to do this where you are using
qVector[index]
only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology).@JonB said in QVector has no assign method?:
if (index >= qVector.size())
qVector.resize(index);Yes that's what I'm doing now. It's a tiny inconvenience.
-
@Publicnamer said in QVector has no assign method?:
Isn't there an automatically resized array?
Such a thing would be extremely dangerous as it would simply resize if the programmer uses an invalid index instead of crashing the application with "out of bounds" exception.
Why do you need such a thing actually?! What is the problem to properly resize the QVector?@jsulm said in QVector has no assign method?:
Such a thing would be extremely dangerous
Not necessarily. It depends on the use case.
-
@jsulm said in QVector has no assign method?:
Such a thing would be extremely dangerous
Not necessarily. It depends on the use case.
@Publicnamer said in QVector has no assign method?:
Not necessarily. It depends on the use case.
Your example above will result in an oom exception after a few milliseconds. If you don't call this dangerous...
-
@JonB said in QVector has no assign method?:
I don't think you can override the [] operator after all?
Why not?
-
@jsulm said in QVector has no assign method?:
I don't think you can override the [] operator after all?
Because
operator[]
is listed among Public functions but does not sayvirtual
?@JonB said in QVector has no assign method?:
Because operator[] is listed among Public functions but does not say virtual?
unlikely you need
operator[]
to be virtual. You normally don't use base classes interfaces for containers.#include<QVector> template <class T> class ExpandingVector : public QVector<T>{ public: using QVector<T>::QVector; T& operator[](int index) { if (index >= QVector<T>::size()) QVector<T>::resize(index+1); return QVector<T>::operator[](index); } };
P.S.
There are many cases where
I still think there is no case where this is useful but you do you bro.
-
@JonB said in QVector has no assign method?:
Because operator[] is listed among Public functions but does not say virtual?
unlikely you need
operator[]
to be virtual. You normally don't use base classes interfaces for containers.#include<QVector> template <class T> class ExpandingVector : public QVector<T>{ public: using QVector<T>::QVector; T& operator[](int index) { if (index >= QVector<T>::size()) QVector<T>::resize(index+1); return QVector<T>::operator[](index); } };
P.S.
There are many cases where
I still think there is no case where this is useful but you do you bro.
@VRonin
This is very useful, but I don't understand :)
If I use your template, but the baseopertaor[]
isn't virtual/overridable, what happens when some other code receiving my array asQVector<>
parameter, notExpandingVector<>
, and then accesses it via[]
? They won't get my oevrride if it wasn'tvirtual
? -
@VRonin
This is very useful, but I don't understand :)
If I use your template, but the baseopertaor[]
isn't virtual/overridable, what happens when some other code receiving my array asQVector<>
parameter, notExpandingVector<>
, and then accesses it via[]
? They won't get my oevrride if it wasn'tvirtual
? -
@JonB That's correct but 3rd party code receiving a
QVector<>
would need to stick to theQVector
contract (i.e.index
must be within bounds).ExpandingVector
will behave exactly likeQVector
in that case. -
Does QVector not have the ability to set the value at any index e.g.
int index = rand(); myvector[index] = rand();
There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.
BTW I see that Qt5 has no QArray. Not sure if there formally was one.
@Publicnamer said in QVector has no assign method?:
And yet a hash isn't good enough.
Why? Can you describe your use-case?
-
@Publicnamer said in QVector has no assign method?:
And yet a hash isn't good enough.
Why? Can you describe your use-case?