We have a legacy method that returns a vector
of char pointers i.e., vector<char *>
.
Now, I need to process only strings (std::string
). How can I do this?
This question may sound simple, but I run into couple of websites which depicted that these sort of considerations might lead to memory leaks.
Now, I either want to get a vector<string>
or even a string without any memory leaks. How can I do this?
The conversion is quite straightforward:
std::vector<char*> ugly_vector = get_ugly_vector();
std::vector<std::string> nice_vector(ugly_vector.begin(), ugly_vector.end());
Once you've done that, though, you still need to make sure that the objects pointed to by the pointers in ugly_vector
are correctly destroyed. How you do that depends on the legacy code you are utilizing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With