STL中常用容器——string容器

概述:C 风格字符串(以空字符结尾的字符数组)太过复杂难于掌握,不适合大程序的开 发,所以 C++标准库定义了一种 string 类,定义在头文件。
String类 和 c 风格字符串对比:
Char*是一个指针,String 是一个类 并且封装了 char,管理这个字符串,是一个 char 型的容器。 String 封装了很多实用的成员方法 查找 find,拷贝 copy,删除 delete 替换 replace,插入 insertstring不用考虑内存释放和越界 string 管理 char*所分配的内存。每一次 string 的复制,取值都由 string 类负责维 护,不用担心复制越界和取值越界等。
1,string类的构造函数

/*1.创建一个空的字符串*/ 
string();
/*2.使用一个 string 对象初始化另一个 string 对象 */
string(const string& str);
/*3.使用字符串 s 初始化*/
string(const char* s);
/*4.使用 n 个字符 c 初始化 v*/ 
string(int n, char c);
string str1;
string str2(str1);
string str3("hello world");
string str4(5, 'c');

2,string 基本赋值操作

/*1.char*类型字符串 赋值给当前的字符串*/
string& operator=(const char* s);
/*2.把字符串 s 赋给当前的字符串*/
string& operator=(const string &s);
/*3.字符赋值给当前的字符串*/
string& operator=(char c);
/*4.把字符串 s 赋给当前的字符串*/
string& assign(const char *s);
/*5.把字符串 s 的前 n 个字符赋给当前的字符串*/
string& assign(const char *s, int n);
/*6.把字符串 s 赋给当前字符串*/
string& assign(const string &s);
/*7.用 n 个字符 c 赋给当前字符串*/
string& assign(int n, char c);
/*8.将 s 从 start 开始 n 个 字符赋值给字符串*/
string& assign(const string &s, int start, int n);

举例:

string str1 = "hello world";
string str2 = str1;
string str3 = 'c';
string str4;
str4.assign("hello world");
string str5;
str5.assign("hello world", 3);
string str6;
str6.assign(str1);
string str7;
str7.assign(3, 'c');
string str8;
str8.assign(str1, 3, 4);

注:可以看出重载等号和assign接口几乎对称。
3,string存取字符操作

/*通过[]方式取字符 */
char& operator[](int n);
/*通过 at 方法获取字符*/
char& at(int n);

举例

	string str("hello");
	cout << str[3] << endl;
	str[2] = '1';
	cout << str.at(4) << endl;

注:重载之后的中括号访问字符使用方法和数组下表索引使用相同。
4,string 拼接操作

/*1.重载+=操作符*/
string& operator+=(const string& str);
/*2.重载+=操作符*/
string& operator+=(const char* str);
/*3.重载+=操作符*/
string& operator+=(const char c);
/*4.把字符串 s 连接到当前字符串结尾*/
string& append(const char *s);
/*5.把字符串 s 的前 n 个字符连接到当前字 符串结尾*/
string& append(const char *s, int n);
/*6.同 operator+=()*/
string& append(const string &s);
/*7.把字符串 s 中从 pos 开始 的 n 个字符连接到当前字符串结尾*/
string& append(const string &s, int pos, int n);
/*8.在当前字符串结尾添加 n 个字符 c*/
string& append(int n, char c);

5,string中的查找和替换
5.1查找

/*1.查找 str 第一次出现位置, 从 pos 开始查找,返回第str中第一个元素在string对象中的索引值*/
int find(const string& str, int pos = 0) const;
/*2.查找 s 第一次出现位置,从 pos 开始查找*/
int find(const char* s, int pos = 0) const;
/*3.从 pos 位置查找 s 的前 n 个 字符第一次位置*/
int find(const char* s, int pos, int n) const;
/*4./查找字符 c 第一次出现位置*/
int find(const char c, int pos = 0) const;
/*5.查找 str 最后一次位置, 从 pos 开始查找*/
int rfind(const string& str, int pos = npos) const;
/*6.查找 s 最后一次出现位置,从 pos 开始查找*/
int rfind(const char* s, int pos = npos) const;
/*7.从 pos 查找 s 的前 n 个字符 最后一次位置*/
int rfind(const char* s, int pos, int n) const;
/*8.查找字符 c 最后一次出现位置*/
int rfind(const char c, int pos = 0) const;

5.2替换

/*1.替换从 pos 开始 n 个 字符为字符串 str*/
string& replace(int pos, int n, const string& str);
/*2.替换从 pos 开始的 n 个字 符为字符串 s*/
string& replace(int pos, int n, const char* s);

6,string 比较操作和string 子串
6.1,string比较操作

/*compare 函数在>时返回 1,<时返回 -1,==时返回 0。 比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的 A 比小写的 a 小。 */
/*1.与字符串 s 比较*/
int compare(const string &s) const;
/*2.与字符串 s 比较*/
int compare(const char *s) const;

6.2,string子串

/*1.返回由 pos 开始的 n 个字符 组成的字符串*/
string substr(int pos = 0, int n = npos) const;

7,string 插入和删除操作

/*1.插入字符串*/
string& insert(int pos, const char* s); 
/*2.插入字符串*/ 
string& insert(int pos, const string& str);
/*3.在指定位置插入 n 个字符 c*/ 
string& insert(int pos, int n, char c); 
/*4.删除从 Pos 开始的 n 个字符*/
string& erase(int pos, int n = npos);

8,string 和 c-style 字符串转换

/*string 转 char* */ 
string str = "hello";
const char* cstr = str.c_str();
/*char* 转 string */ 
char* s = "hello"; 
string str(s);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值