STL学习笔记(3)常用容器 string

string 容器基本概念

C风格字符串(以空字符结尾的字符数组)太过复杂难于掌握,不适合大程序的开发,所以C++标准库定义了一种string 类,定义在头文件。

String 和 c 风格字符串对比:

  • char*是一个指针,String 是一个类
  • string 封装了 char,管理这个字符串,是一个 char 型的容器。
  • String 封装了很多实用的成员方法 查找 find,拷贝 copy,删除 delete 替换 replace,插入 insert
  • 不用考虑内存释放和越界
  • string 管理 char*所分配的内存。每一次 string 的复制,取值都由string类负责维护,不用担心复制越界和取值越界等算法

string 容器常用操作

1. string 构造函数
string();//创建一个空的字符串 例如: string str; 
string(const string& str);//使用一个 string 对象初始化另一个 string 对象 
string(const char* s);//使用字符串 s 初始化 
string(int n, char c);//使用 n 个字符 c 初始化 v 
2. string 基本赋值操作
string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串 
string& operator=(const string &s); //把字符串 s 赋给当前的字符串 
string& operator=(char c); //字符赋值给当前的字符串 
string& assign(const char *s); //把字符串 s 赋给当前的字符串 
string& assign(const char *s, int n); //把字符串 s 的前 n 个字符赋给当前的字符串 
string& assign(const string &s); //把字符串 s 赋给当前字符串 
string& assign(int n, char c); //用 n 个字符 c 赋给当前字符串 
string& assign(const string &s, int start, int n); //将 s 从 start 开始 n 个字符赋值给字符串 
3. string 存取字符操作
char& operator[](int n); //通过[]方式取字符 
char& at(int n); //通过 at 方法获取字符 
4. string 拼接操作
string& operator+=(const string& str); //重载+=操作符 
string& operator+=(const char* str); //重载+=操作符 
string& operator+=(const char c); //重载+=操作符 
string& append(const char *s); //把字符串 s 连接到当前字符串结尾 
string& append(const char *s, int n);//把字符串 s 的前 n 个字符连接到当前字符串结尾 
string& append(const string &s);//同 operator+=() 
string& append(const string &s, int pos, int n);//把字符串 s 中从 pos 开始的 n 个字符连接到当 前字符串结尾 
string& append(int n, char c);//在当前字符串结尾添加 n 个字符 c 
5. string 查找和替换
int find(const string& str, int pos = 0) const; //查找 str 第一次出现位置,从 pos 开始查找 
int find(const char* s, int pos = 0) const; //查找 s 第一次出现位置,从 pos 开始查找 
int find(const char* s, int pos, int n) const; //从 pos 位置查找 s 的前 n 个字符第一次位置 
int find(const char c, int pos = 0) const; //查找字符 c 第一次出现位置 
int rfind(const string& str, int pos = npos) const;//查找 str 最后一次位置,从 pos 开始查找 
int rfind(const char* s, int pos = npos) const;//查找 s 最后一次出现位置,从 pos 开始查找 
int rfind(const char* s, int pos, int n) const;//从 pos 查找 s 的前 n 个字符最后一次位置 
int rfind(const char c, int pos = 0) const; //查找字符 c 最后一次出现位置 
string& replace(int pos, int n, const string& str); //替换从 pos 开始 n 个字符为字符串 str 
string& replace(int pos, int n, const char* s); //替换从 pos 开始的 n 个字符为字符串 s
6. string 比较操作
/*
compare 函数在>时返回 1,<时返回 -1,==时返回 0。 
比较区分大小写,比较时参考字典顺序,排越前面的越小。 大写的 A 比小写的 a 小。 
*/
int compare(const string &s) const;//与字符串 s 比较 
int compare(const char *s) const;//与字符串 s 比较 
7. string 子串
string substr(int pos = 0, int n = npos) const;//返回由 pos 开始的 n 个字符组成的字符串 
8. string 插入和删除操作
string& insert(int pos, const char* s); //插入字符串 
string& insert(int pos, const string& str); //插入字符串 
string& insert(int pos, int n, char c);//在指定位置插入 n 个字符 c 
string& erase(int pos, int n = npos);//删除从 Pos 开始的 n 个字符 
9. string 和 c-style 字符串转换
//string 转 char* 
string str = "itcast"; 
const char* cstr = str.c_str(); 

//char* 转 string 
char* s = "itcast"; 
string str(s);

在 c++中存在一个从 const char 到 string 的隐式类型转换,却不存在从一个 string 对象到 Cstring 的自动类型转换。

对于 string 类型的字符串,可以通过 c_str()函数返回 string 对象对应的 C_string。通常,程序员在整个程序中应坚持使用 string 类对象,直到必须将内容转化为 char 时才将其转换为 C_string.

提示: 为了修改 string 字符串的内容,下标操作符[]和 at 都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误

string s = "abcdefg"; 
char& a = s[2]; 
char& b = s[3]; 
a = '1'; 
b = '2'; 
cout << s << endl; 
cout << (int*)s.c_str() << endl; 
s = "pppppppppppppppppppppppp"; 

//a = '1';
//b = '2'; 
cout << s << endl; 
cout << (int*)s.c_str() << endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻舞飞扬SR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值