I am trying to copy 5 characters from a character array into a std::string
char name[] = "Sally Magee";
std::string first;
copy(name, name + 5, first.begin()); //from #include <algorithm>
std::cout << first.c_str();
However I get the string plus a whole bunch of unprintable characters that I do not want. Any ideas? Thanks.
Just do
char name[] = "Sally Magee";
std::string first(name, name + 5);
std::cout << first << std::endl;
see std::string constructor link
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