目录
2.2 string 容器
2.2 string 容器
2.2.1 string 的特性
说到 string 的特性,就不得不和 char*类型的字符串的对比:
- Char*是一个指针,String 是一个类 string 封装了 char*,管理这个字符串,是一个 char*型的容器。
- String 封装了很多实用的成员方法 查找 find,拷贝 copy,删除 delete 替换 replace,插入 insert
- 不用考虑内存释放和越界 string 管理 char*所分配的内存。每一次 string 的复制,取值都由 string 类负责维护, 不用担心复制越界和取值越界等。
string 和 char*可以互相转换吗?如果能,怎么转换呢? 答案是可以转换。string 转 char*通过 string 提供的 c_str()方法
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string sstr(s);
2.2.2 string 常用 API
2.2.2.1 string 构造函数
string();//创建一个空的字符串 例如: string str;
string(const string& str);//使用一个 string 对象初始化另一个 string 对象
string(const char* s);//使用字符串 s 初始化
string(int n, char c);//使用 n 个字符 c 初始化
//例子:
//默认构造函数
string s1;
//拷贝构造函数
string s2(s1);
string s2 = s1;
//带参数构造函数
char* str = "itcast";
string s3(str);
string s4(10,
2.2.2.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个字符赋值给字符串
2.2.2.3 string 存取字符操作
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过 at 方法获取字符
//例子:
string s = "itcast";
char c = s[1];
c = s.at(1);
问:string 中存取字符[]和 at 的异同?
答: 1 相同, [ ] 和 at 都可以返回第 n 个字符
2 不同,at 访问越界会抛出异常,[ ] 越界会直接程序会挂掉
2.2.2.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
//例子
string s2="1111";
string s3="2222";
s2.append(s3);
cout<<s2<<endl;
2.2.2.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
//例子
string s="abcdefghij";
//第一次出现的位置
int pos=s.find("fg");
cout<<" pos: "<<pos<<endl;
//第二次出现的位置
pos=s.rfind("fg");
cout<<" pos : "<<pos<<endl;
string s="abcdefg";
s.replace(0,2,"111");
cout<<s<<endl;
2.2.2.6 string 比较操作
/*
compare 函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的 A 比小写的 a 小。
*/
int compare(const string &s) const;//与字符串 s 比较
int compare(const char *s) const;//与字符串 s 比较
//例子
string s1="abcd";
string s2="abce";
if(s1.compare(s2)==0) cout<<"字符串相等"<<endl;
else cout<<"符串不相等" <<endl;
2.2.2.7 string 返回子串操作
string substr(int pos = 0, int n = npos) const;//返回由 pos 开始的 n 个字符组成的字符串。
//字串操作例子
string s="abcdefgh";
string mysubstr=s.substr(1,3);
cout<<mysubstr<<endl;
2.2.2.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 个字符
//例子
string s="abcdefg";
s.insert(3,"111");
cout<<s<<endl;
s.erase(0,2);
cout<<s<<endl;
编写不易,大家来个三联再走吧