1.string类介绍
C++的string属于STL(Standard Template Library, 标准模板库)中的定义的类。
程序中使用string类,必须包含头文件 。如下:
#include <string>
string是一个模板类,位于std命名空间内,为方便使用还需要在程序中增加:
using namespace std; // 指定缺省的使名空间。
string str; // 创建string对象。
如果不指定命名空间,也就是说没有using namespace std,创建string对象的方法如下:
std::string str;
注意这里不是string.h,string.h是C语言字符串头文件。
2. string的重载的操作符
可以用=直接赋值。
可以用 ==、>、<、>=、<=、和!=比较字符串。
可以用+或者+=操作符连接两个字符串。
可以用[]获取指定位置的字符,类似数组。