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
Post a Comment