For example
using namespace std;
array<vector<int>, 3> a;
vector<int> v0, v1, v2;
// assign with move
a[0] = move(v0);
a[1] = move(v1);
a[2] = move(v2);
How to use one line to implement the assignment like a = {v0, v1, v2}
?
For example
using namespace std;
array<vector<int>, 3> a;
vector<int> v0, v1, v2;
// assign with move
a[0] = move(v0);
a[1] = move(v1);
a[2] = move(v2);
How to use one line to implement the assignment like a = {v0, v1, v2}
?
If you want to initialize a fresh std::array
, array<vector<int>, 3> a = { move(v0), move(v1), move(v2) }
would always work, otherwise construct a temporary std::array
and then use std::move in <algorithm>
:
array<unique_ptr<int>, 3> temp_arr = { move(a), move(b), move(c) };
move( begin(temp_arr), end(temp_arr), begin(arr) );
@user1899020 : You may refer to this What is std::move(), and when should it be used?
Please note "move semantics are performed transparently to the programmer. move its only a cast to pass a value from one point to another where the original lvalue will no longer be used."
So in below code:
a= {move(v0), move(v1), move(v2)};
for(int i=0; i <3; i++)
{
// Iterate and print values of vector
for(int n : a[i]) {
std::cout << n << '\n';
}
}
**//Code will NOT enter below for loop**
for (std::vector<int>::const_iterator j= v0.begin(); j != v0.end(); j++)
std::cout << *j << ' ';
So if you want to use the retain the values in original vector,
a = {v0, v1, v2};
another way could be using the pointers by initialising array as below:
array<vector<int> *, 3> a = {&v0, &v1, &v2};
Write a tuple_assign
function:
template <class T>
using uncvref = std::remove_cv_t<std::remove_reference_t<T>>;
template <class T, class U, std::size_t...Is>
void tuple_assign_(T& to, U&& from, std::index_sequence<Is...>) {
std::initializer_list<int>{
(std::get<Is>(to) = std::get<Is>(std::forward<U>(from)), 0)...
};
}
template <class T, class U,
std::size_t TSize = std::tuple_size<uncvref<T>>::value,
std::enable_if_t<TSize == std::tuple_size<uncvref<U>>::value, int*> = nullptr>
T& tuple_assign(T& to, U&& from) {
tuple_assign_(to, std::forward<U>(from), std::make_index_sequence<TSize>{});
return to;
}
and wrap the vectors with forward_as_tuple
(DEMO):
tuple_assign(a, std::forward_as_tuple(std::move(v0), std::move(v1), std::move(v2)));