Is there a more elegant way to initialise the second string with a single char from the first string? Eg. without resorting to the string ( size_t n, char c ) constructor?
string first = "foobar";
string second(string(1, first[0]));
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The constructor you mention is the way to create a string from one character, so there won’t be a significantly more elegant way. However, there’s no need to create and copy/move a temporary:
Alternatively, you could construct from a substring of
first:In C++11, you can use an initialiser list: