qtqvector

is there a better way to append a QVector for 'zero padding'?


I have a QVector (v) of length (n), which has multiple values already in it. I want to double the length of this QVector and populate the second half with 0s. Is there a better way to do this than:

for (int i=0;i<n;i++) {
    v.append(0);
}

It just feels inefficient/inelegant to be calling append in a loop

Thanks in advance...


Solution

  • Solution

    Simply double the length by calling v.resize( 2*v.length() ). That's all.

    Explanation

    The documentation says that QVector<T>::resize() initializes the new elements with a default-constructed value. When T is a numeric type, the default value is 0.