Initialize QVector from array


I receive an array as a pointer from a function and want to initialize a QVector from that.

For now I do it like this:

void foo(double* receivedArray, size_t size) { QVector<double> vec(size); std::copy(receivedArray, receivedArray + size, std::begin(vec)); }

Would it be equally possible to do this:

void foo(double* receivedArray, size_t size) { QVector<double> vec(size); vec.data() = receivedArray; }

Would this break some kind of Qt mechanism that I am not aware of?


The first one does unnecessary work, initializing the vector with default-constructed doubles before filling it. Unfortunately, QVector lacks a ranged-insertion, so you must resort to algorithms:

void foo(double* receivedArray, size_t size) { QVector<double> vec; vec.reserve(size); // warning: size_t->int cast std::copy(receivedArray, receivedArray + size, std::back_inserter(vec)); }

The second version does not even compile, as data() returns a T *, which is a rvalue that you can't put on the left side of an assignment.


<a href="http://doc.qt.io/qt-5/qvector.html#data" rel="nofollow">QVector::data</a> does not return a reference to the underlying pointer, so you cannot assign to vec.data() (it is not an lvalue, it will not even compile):

template <typename T> struct Vector { T* data_; T* nref_data () { return data_; } T* &ref_data () { return data_; } }; Vector<int> vec; vec.ref_data() = new int[100]; // Ok, Vector<int>::ref_data returns a reference vec.nref_data() = new int[100]; // Nok, Vector<int>::nref_data does not return a reference


  • What is “User stylesheet” in Google Chrome's developer tools?
  • High level client for golang http2 server push
  • localStorage data persistence
  • How to check Url Image is exist or not in php [duplicate]
  • How to correctly remove whatever is necessary from backbone view?
  • Udp image streaming, delphi indy10
  • Pandas better way to get rows that has all columns null except one
  • When hibernate performs sync between second level cache and DB with various Cache modes?
  • Password_verify in PHP
  • Python SKLearn: How to Get Feature Names After OneHotEncoder?
  • Intent-Filter within a Service
  • short and easy question on spring nested transactions
  • jre_home environment variable is not defined correctly while starting tomcat
  • File structure for PHP-based website
  • exception thrown while building the java application using netbeans
  • Problem with installing Charm-Crypto for Python3
  • How to set title name of the pdf. While viewing the Document(New Tab)
  • Create a mobile app that just points to URL
  • Installation failed for laravel/lumen-installer: guzzlehttp/guzzle locked at 6.3.0
  • How to use grep to output unique lines of code from a file?
  • Max execution time error
  • get all files in git diff in intellij
  • Working with codeception and laravel
  • How to smoothly connect two signals in matlab [closed]
  • Refresh JSF component after custom javascript Ajax call
  • Use 2D Text into 3D scenes in JavaFX results in blurry texts
  • How to search for a method or variable name within android project?
  • Modifying native query cannot have named parameter bindings?
  • Adding native code to an existing Worklight hybrid app
  • Swift manually rotate view controller
  • Debugging php script timeout?
  • time column in sqlite using gorm
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Can I read another applications memory?
  • how do i compare two rows and store the similarities of the two rows in another column
  • How to warp text around image in iOS?
  • php “page caching” solution suggestions for CMS Applications
  • Jersey serializes character value to ASCII equivalent numeric string
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?