如何在C ++中将单个字符转换为字符串?
有几种方法可以将单个字符转换为字符串。在下面的示例中,其中一些用于将字符转换为字符串。
这是使用C++语言将单个字符转换为字符串的示例,
示例
#include <iostream>
#include<string>
#include<sstream>
int main() {
char c = 'm';
std::string s(1, c);
std::cout << "Using string constructor : " << s << '\n';
std::string s2;
std::stringstream s1;
s1 << c;
s1 >> s;
std::cout << "Using string stream : " << s << '\n';
s2.push_back(c);
std::cout << "Using string push_back : " << s2 << std::endl;
return 0;
}输出结果
这是输出
Using string constructor : m Using string stream : m Using string push_back : m
在上面的程序中,使用三种方法将字符转换为字符串。一,使用字符串构造器
std::string s(1, c);
二,使用字符串流
std::string s2; std::stringstream s1; s1 << c; s1 >> s;
第三,使用字符串push_back
s2.push_back(c);
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿