在C++中,输出字符串可以使用std::cout或者std::printf函数来实现
在C++中,输出字符串可以使用std::cout
或者std::printf
函数来实现。
使用std::cout
输出字符串的方法如下:
#include<iostream> intmain(){ std::stringstr="Hello,World!"; std::cout<<str<<std::endl; return0; }
使用std::printf
输出字符串的方法如下:
#include<iostream> intmain(){ std::stringstr="Hello,World!"; std::printf("%s\n",str.c_str()); return0; }
注意:在使用std::printf
输出字符串时,需要通过c_str
方法将std::string
类型转换为C风格的字符串。
版权声明
本文仅代表作者观点,不代表博信信息网立场。