STL学习(一、string容器)
目录
子串获取
字符串插入和删除
插入
删除
字符串读写
字符串比较
字符串查找和替换
字符串拼接
赋值操作
构造函数
1.子串获取
2.字符串插入和删除
插入
函数原型
string & insert(int pos, const char *s); // 插入字符串
string & insert(int pos, const string &str); // 插入string类
删除
函数原型
string &erase(int pos, int n = npos) // 删除从pos开始的npos字符
3.字符串读写
函数原型
char & operator[](int n); // 通过[]方式取字符
4.字符串比较
函数原型
int compare(const string &s) const; // string类比较
int compare(const char * s) const; // 字符串比较
相等返回0,>返回1,<返回-1,但是一般只用来比较两个字符串是否相等
5.字符串查找和替换
int find(const string &str, int pos = 0) // 从pos开始查找 string第一次出现的位置
int find(const char * s, int pos = 0) // 从pos开始查找字符串第一次出现的位置
int find(const char c, int pos = 0) // 从pos开始查找字符c第一次出现的位置
int rfind(const string &str, int pos = 0) // 从pos开始查找 string最后一次出现的位置
int rfind(const char * s, int pos = 0) // 从pos开始查找字符串最后一次出现的位置
int rfind(const char c, int pos = 0) // 从pos开始查找字符c最后一次出现的位置
6.字符串拼接
函数原型
string & operator+=(const char*str) // 在最后拼接字符串
string & operator+=(const char c) // 在最后添加字符
string & operator+=(const string & str) // 在最后添加string类
7.赋值操作
函数原型(全部都是深拷贝)
string & operator=(const char *s) // 字符串赋值
string & operator=(const string &s) // string 赋值
string & operator=(char c) //字符赋值
8.构造函数
函数原型(全部深拷贝)
string() // 空字符串
string(const char *s) // 使用字符串初始化
string (const string &str) // 使用string 初始化