#include <stdio.h>
#include <string.h>
#include <typeinfo>
using namespace std;
int main()
{
string str="a";
char s[100]={"hello \0 world !"};
char *cp="\0";
const char * ccp;
str=s;
//s=str;
printf("%s",s);
cout << str<< endl;
cout << strlen(s);
cout<<str.length();
str+="wo\0rld";
cout <<str;
cout<<str.length()<<endl;
cout << "string类型"<<endl;
cout << typeid(str).name()<<endl;
cout<<"字符串数组类型"<<endl;
cout <<typeid(s).name()<<endl;
cout<<"字符指针类型"<<endl;
cout <<typeid(cp).name()<<endl;
cout<<"const char *类型"<<endl;
cout <<typeid(ccp).name()<<endl;
cout<<"c_str()类型"<<endl;
cout <<typeid(str.c_str()).name()<<endl;
str=cp;
// ss=str.c_str();
strcpy(s,str.c_str());
ccp=str.c_str();
printf("%s",str.c_str());
//scanf("%s",&str);
// cout<< str.c_
cout<< str.c_str();
cout <<endl;
return 0;
}
// 可以将字符串数组赋值给string
// 字符串也是以'\0'作为结束标志的
// 调用string的c_str()函数返回const char * 指针,该指针不能直接赋值给字符指针或字符数组,需要用strcpy赋值
//%
c 字符串与C++string
最新推荐文章于 2024-09-12 00:41:57 发布