c++输出字符串的有哪些方法
在C++中,输出字符串可使用std::cout还是std::printf函数来实现。
使用std::cout输出字符串的方法以下:
#include <iostream>
int main() {
std::string str = "Hello, World!";
std::cout << str << std::endl;
return 0;
}
使用std::printf输出字符串的方法以下:
#include <iostream>
int main() {
std::string str = "Hello, World!";
std::printf("%s
", str.c_str());
return 0;
}
注意:在使用std::printf输出字符串时,需要通过c_str方法将std::string类型转换为C风格的字符串。
TOP