Const double * to QVector<double>



  • Hello,
    i got a pointer to an array with double values. But i need the values in a QVector.

    const double *array = externObject->getArray();
    
    QVector<double> myArray = array;// how to solve this problem
    

    I can do a for loop, but it doesn't look very well:

    QVector<double> myArray;
    for(int i = 0; i < externObject->arrayCount(); i++) {
      myArray << externObject->getArray()[i];
    }
    

    Is there a better way to do that?

    Thanks


  • Qt Champions 2016

    @beecksche

    int count = externObject->arrayCount();
    const double * array = externObject->getArray();
    
    QVector<double> myArray(count);
    ::memcpy(myArray.data(), array, count * sizeof(double));
    


  • your solution is more than acceptable, an alternative is:

    QVector<double> myArray(externObject->arrayCount());
    std::copy(array,array + externObject->arrayCount(),myArray.begin());
    

  • Moderators

    @beecksche
    you implicitly already showed that it isn't possible with an assignment operator only. (Edit: but i guess that's not what you were after afterall?)
    In your loop you used arrayCount() to determine the item count. How would you do it in the assignment operator?



  • Thanks everybody for the replies.

    I recognized that i need to modify some values. However the loop seems the best way! Thanks a lot!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.