how to convert/copy a vector to unique_ptr<int[]> ?
this is what I want to achieve (the vector originates in another context and will be deleted after copying to the unique_ptr. unique_ptr is of fixed length and thus can be allocated more efficiently).
std::vector<int> vv {1, 2, 3};
struct
U {
U(std::vector<int> v) { // copy constructor
// copy vector v to unique_ptr u, HOW?
}
std::unique_ptr<int[]> u;
};
U uu {vv}; // copy vector vv to unique_ptr uu, HOW?
i tried to make a copy constructor
U(std::vector<int> v) {
u = std::make_unique<int[]>(v.size()); // OK
std::copy(v.begin(), v.end(), u); // error: u has no copy constructor
}
so, how should I construct the copy constructor?
actually and moreover, once constructed I want the unique_ptr<T[]> to be constant in both length and content, so in my case it should be
std::unique_ptr<int const[]> const u; // const in both length and content
how does the copy constructor look in both cases?
case 1) std::unique_ptr<int[]> u case 2) std::unique_ptr<int const[]> const u
std::vector
and astd::unique_ptr
. Use a vector, it's the tool for the job. – NathanOlivervector
compared tounique_ptr
so this should be clearified first. – t.niesereserve
which will be allocated just as efficiently. – Eljay