Q:

How to convert an array of string pointers into a vector of smart pointers of type string?

I have a code like this:
std::string* string_ptr[] = { new std::string("x"), new std::string("y"), new std::string("z") }; 

I need to get a vector like this:
std::vector<std::unique_ptr<string>> vec;

I also need to clear memory in the original string_ptr array and in the new vector. 
How to do it better?

A:

If u want to transfer the ownership to a vector, you can do as follows (after this u won't have to free any memory. the vector will manage that)
int main() {
    std::string* string_ptr[] = { new std::string("x"), new std::string("y"), new std::string("z") }; // step 1
    size_t sze = size(string_ptr);

    std::vector<std::unique_ptr<std::string>> vec(sze); // step 2
    for(size_t i{}; i < sze; ++i){
        vec[i].reset( string_ptr[i]);
        string_ptr[i] = nullptr;//transfer the elments ownership
    }
}

If you want to copy them only(you will have to manage the memory held by the raw pointers)
int main() {
    std::string* string_ptr[] = { new std::string("x"), new std::string("y"), new std::string("z") }; // step 1
    size_t sze = size(string_ptr);
    std::vector<std::unique_ptr<std::string>> vec(sze); // step 2
    for(size_t i{}; i < sze; ++i){
        ;
        vec[i].reset(new std::string{*string_ptr[i]});
    }
    vec.erase(vec.end()-1);
}

See Why can I not push_back a unique_ptr into a vector?

