Creating a std::string from a Single Character
You have a single char value and aim to convert it into a std::string. This process involves transforming the character into a string variable.
To achieve this, you can employ various methods:
std::string Constructor with Length Parameter:
std::string with Brace-Enclosed Initializer:
push_back() Method:
Example:
<code class="cpp">char c = 34; std::string s1(1, c); std::string s2{c}; std::string s3; s3.push_back(c); std::cout << s1 << std::endl; std::cout << s2 << std::endl; std::cout << s3 << std::endl;</code>
Output:
" " "
The above is the detailed content of How to Convert a Single Character to a `std::string` in C ?. For more information, please follow other related articles on the PHP Chinese website!