C++ 中 printf输出string字符串的方法

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;
}

输出如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41962612/article/details/115250491