c++ - std::vector::assign equivalent for QVector -


i have c-style array, , want assign qvector. if using std::vector, have used assign():

int arr[] = { 1, 2, 3, 4, 5 }; std::vector<int> v; v.assign(arr, arr + sizeof(arr)/sizeof(int)); 

but qvector couldn't find similar assign() method, or range-accepting constructor.

i've coded for-loop it, surprised there wasn't such basic function, so, or there equivalent?

you can use qt's qcopy():

int arr[] = { 1, 2, 3, 4, 5 }; qvector<int> v(5); qcopy(arr, arr+5, v.begin()); 

or can use std::copy() of course.

int arr[] = { 1, 2, 3, 4, 5 }; qvector<int> v(5); std::copy_n( &arr, 5, v.begin() ); // or more general: qvector<int> v2; std::copy(std::begin(arr), std::end(arr), std::back_inserter(v2)); // without begin/end std::copy(arr, arr + 5, std::back_inserter(v2)); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -