#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
//获取字符串长度
string str = "hello";
int lens1 = str.length();
int lens2 = str.size();
cout << lens1 << endl;
cout << lens2 << endl;
//字符串连接
string str1 = "he";
string str2 = "llo";
str = str1 + str2;
cout << str << endl;
//字符串比较
if( str1 != str2 )
{
cout << "not equal" << endl;
}
//字符串转字符数组
char *res = new char[10];
strcpy( res , str.c_str() );
cout << res << endl;
delete []res;
return 0;
}
结果如下:

string还有好多其他的用法,但对于C语言转C++的用法来说,这些比较常用

被折叠的 条评论
为什么被折叠?



