this is quite a simple question, however I'm finding it tricky. I want to treat a char*
as if it were a std::string
, for instance:
char *p = ...; // read a huge chuck from a file
std::string s(p); // this is not what I want
So, if I use the constructor, I get a copy of p, which is a waste of memory and time. Is it possible somehow to avoid this, and "assign" the std::string
content to a pre-existing address?
Any other idea is more than welcome!
Thanks!
We can convert a char to a string object in java by using the Character. toString() method.
Syntax: char* strcpy (char* destination, const char* source); The strcpy() function is used to copy strings. It copies string pointed to by source into the destination . This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination .
Using the inbuilt function strcpy() from string. h header file to copy one string to the other. strcpy() accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string.
Is it possible somehow to avoid this, and "assign" the
std::string
content to a pre-existing address?
No.
However, you can assign it to a std::string_view
. Going forward, all uses of std::string
except to own memory should be replaced by std::string_view
.
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