C/C++ String类用法与心得
条评论String类简介
String
是以char
作为模板参数的模板类实例,把字符串的内存管理责任由string
负责而不是由编程者负责,大大减轻了C语言风格的字符串的麻烦。1
String
的特色:
- 提供了大量的字符操作函数。
- 可与C语言风格字符串双向转换。2
成员函数简介与常用代码写法
头文件与声明
1 |
注意这里不是string.h string.h是C字符串头文件
1 | string str; //声明一个空字符串 |
输入输出操作
1 | cin>>str; //读入有效字符直到遇到空格 |
字符串赋值操作
string &operator=(const string &s);
把字符串s赋给当前字符串string &assign(const char *s);
用c类型字符串s赋值string &assign(const char *s,int n);
用c字符串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个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);
把first和last迭代器之间的部分赋给字符串
举例:3
1 | // string::assign |
在尾部添加字符
string &operator+=(const string &s);
把字符串s连接到当前字符串的结尾string &append(const char *s);
把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);
把c类型字符串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个字符cstring &append(const_iterator first,const_iterator last);
把迭代器first和last之间的部分连接到当前字符串的结尾
下面为举例:3
1 | // appending to string |
另外string还可以用push_back()
来添加单个字符
1 | s.push_back('a');//这个函数只能增加单个字符 |
String类的插入函数
string &insert(int p0, const char *s);
在p0位置插入字符串sstring &insert(int p0, const char *s, int n);
在p0位置插入字符串s的前n个字符string &insert(int p0,const string &s);
在p0位置插入字符串sstring &insert(int p0,const string &s, int pos, int n);
在p0位置插入字符串s中pos开始的n个字符string &insert(int p0, int n, char c);
此函数在p0处插入n个字符citerator insert(iterator it, char c);
在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);
在it处插入[first,last)
之间的字符void insert(iterator it, int n, char c);
在it处插入n个字符c
举例如下:3
1 | // inserting into a string |
String类的删除函数
iterator erase(iterator first, iterator last);
删除[first,last)
之间的所有字符,返回删除后迭代器的位置iterator erase(iterator it);
删除it指向的字符,返回删除后迭代器的位置string &erase(int pos = 0, int n);
删除pos开始的n个字符,返回修改后的字符串
举例:3
1 | // string::erase |
String类的替换函数
string &replace(int p0, int n0,const char *s);
删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const char *s, int n);
删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符string &replace(int p0, int n0,const string &s);
删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const string &s, int pos, int n);
删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符string &replace(int p0, int n0,int n, char c);
删除p0开始的n0个字符,然后在p0处插入n个字符cstring &replace(iterator first0, iterator last0,const char *s);
把[first0,last0)之间的部分替换为字符串sstring &replace(iterator first0, iterator last0,const char *s, int n);
把[first0,last0)之间的部分替换为s的前n个字符string &replace(iterator first0, iterator last0,const string &s);
把[first0,last0)之间的部分替换为串sstring &replace(iterator first0, iterator last0,int n, char c);
把[first0,last0)之间的部分替换为n个字符cstring &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);
把[first0,last0)之间的部分替换成[first,last)之间的字符串
1 | // replacing in a string |
String的子串
string substr(int pos = 0, int n) const;
返回pos开始的n个字符组成的字符串
1 | string hello = s.substr(9, 5); //hello |
String类的查找函数
int find(char c, int pos = 0) const;
从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;
从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;
从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;
从pos开始查找字符串s在当前串中的位置
除了find()
外,还有
rfind()
:从pos开始从后向前查找字符串s在当前串中的位置
find_first_of()
从pos开始查找当前串中第一个出现的串位置
find_first_not_of()
从当前串中查找第一个不在串s中的字符出现的位置
find_last_of()
和find_last_not_of()
从后向前找
更多的情况下,我们可以直接把函数和npos进行比较:
1 | if(s.find(hello) != string::npos){ |
完整代码
1 |
|
基本操作函数汇总
函数 | 用法 |
---|---|
begin() | 返回指向起始的迭代器 |
clear() | 删除所有字符 |
empty() | 检查字符串是否为空 |
end() | 返回指向尾端的迭代器 |
erase() | 删除字符 |
insert() | 插入字符 |
max_size() | 返回可以容纳的最大字符个数 |
rbegin() | 返回指向起始的逆向迭代器 |
rend() | 返回指向末尾的逆向迭代器 |
size() | 返回容纳的字符数 |
swap() | 交换内容 |
find() | 寻找带有特定键的元素 |
push_back() | 后附字符到结尾 |
pop_back() | 移除末尾字符 |
append() | 后附字符到结尾 |
operator+= | 后附字符到结尾 |
compare() | 比较两个字符串 |
replace() | 替换字符串的指定部分 |
substr() | 返回子串 |
copy() | 复制字符 |
resize() | 更改存储的字符数 |
find() | 在字符串中查找字符 |
rfind() | 寻找子串的最后一次出现 |
find_first_of() | 寻找子串的第一次出现 |
find_first_not_of() | 寻找子串的第一次缺失 |
find_last_of() | 寻找子串的最后一次出现 |
find_last_not_of() | 寻找子串的最后一次缺失 |
npos | 特殊值 |
- 本文链接:C/C++ String类用法与心得
- 发布时间:2019年02月26日 - 9:14:43
- 更新时间:2021年02月03日 - 6:56:56
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!
分享