Question

1) I want to pass a the pointer of a QVector to a function and then do things with it. I tried this:

void MainWindow::createLinearVector(QVector<float> *vector, float min, float max )
{
    float elementDiff=(max-min)/(vector->size()-1);

    if(max>min) min -= elementDiff;
    else        min += elementDiff;

    for(int i=0; i< vector->size()+1 ; i++ )
    {
        min += elementDiff;
        *(vector+i) = min; //Problematic line
    }

}

However the compiler gives me "no match for operator =" for the *(vector+i) = min; line. What could be the best way to perform actions like this on a QVector?

2) The function is supposed to linearly distribute values on the vector for a plot, in a way the matlab : operator works, for instance vector(a:b:c). What is the simpliest and best way to perform such things in Qt?

EDIT:

With help from here the initial problem is solved. :)

I also improved the metod in itself. The precision could be improved a lot by using linear interpolation instead of multiple additions like above. With multiple addition an error is accumulating, which is eliminated in large part by linear interpolation.

Btw, the if statement in the first function was unecessary and possible to remove by just rearranging stuff a little bit even in the multiple addition method.

void MainWindow::createLinearVector(QVector<double> &vector, double min, double max )
{
    double range = max-min;
    double n =   vector.size();

    vector[0]=min;

    for(int i=1; i< n ; i++ )
    {
        vector[i] = min+ i/(n-1)*range;
    }
}

I considered using some enchanced loop for this, but would it be more practical? With for instance a foreach loop I would still have to increment some variable for the interpolation right? And also make a conditional for skipping the first element?

No correct solution

OTHER TIPS

I want to place a float a certain place in the QVector.

Then use this:

(*vector)[i] = min; //Problematic line

A vector is a pointer to a QVector, *vector will be a QVector, which can be indiced with [i] like any QVector. However, due to precedence, one needs parentheses to get the order of operations right.

I think, first u need use the Mutable iterator for this stuff: Qt doc link

Something like this:

QMutableVectorIterator<float> i(vector);
i.toBack();
while (i.hasPrevious())
    qDebug() << i.{your code}

Right, so it does not make much sense to use a QVector pointer in here. These are the reasons for that:

  • Using a reference for the method parameter should be more C++'ish if the implicit sharing is not fast enough for you.

  • Although, most of the cases you would not even need a reference when just passing arguments around without getting the result back in the same argument (i.e. output argument). That is because *QVector is implicitly shared and the copy only happens for the write as per documentation. Luckily, the syntax will be the same for the calling and internal implementation of the method in both cases, so it is easy to change from one to another.

  • Using smart pointers is preferable instead of raw pointers, but here both are unnecessarily complex solutions in my opinion.

So, I would suggest to refactor your code into this:

void MainWindow::createLinearVector(QVector<float> &vector, float min, float max)
{
    float elementDiff = (max-min) / (vector.size()-1);

    min += ((max>min) ? (-elementDiff) : elementDiff)

    foreach (float f, vector) {
        min += elementDiff;
        f = min;
    }
}

Note that I fixed up the following things in your code:

  • Reference type parameter as opposed to pointer

  • "->" member resolution to "." respectively

  • Ternary operation instead of the unnatural if/else in this case

  • Qt's foreach instead of low-level indexing in which case your original point becomes moot

This is then how you would invoke the method from the caller:

createLinearVector(vector, fmin, fmax);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top