此函数返回一个指向包含空终止字符序列的数组的指针。
str.c_str();
它不包含任何参数。
它返回一个指向字符串对象值的c字符串表示形式的指针。
#include<iostream> #include<cstring> #include<string> using namespace std; int main() { string str="Computer is my favorite subject"; char* ch=new char[str.length()+1]; strcpy(ch,str.c_str()); cout<<"String value is :"<<ch; return 0; }
输出:
String value is: Computer is my favorite subject
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)