C++ 中 printf输出string字符串不能直接printf("%s",str);非常不方便,一个一个字符输出也不现实。
这里可以借助str.c_str()
函数对字符串str进行转换,再输出。
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
string str="123abc";
cout<<str<<endl;
printf("%s\n",str.c_str());
return 0;
}
输出如下