零、前言
本章主要讲解C++string类的相关知识以及使用,还会模拟实现一下string类
一、什么是string类
- 引入:
C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问,由此C++做出改进引入了string类
概念:
string是表示字符串的字符串类
该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作
string在底层实际是:basic_string模板类的别名
typedef basic_string<char, char_traits, allocator>string;
- 不能操作多字节或者变长字符的序列
注:在使用string类时,必须包含
#include<string> using namespace std;
二、string类常用接口说明
注:下面讲解最常用的接口
1、string类对象常见构造
函数名称 | 功能说明 |
string() (重点) | 构造空的string类对象,即空字符串 |
string(const char* s) (重点) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string&s) (重点) | 拷贝构造函数 |
string (const string& str, size_t pos, size_t len = npos) | 从str对象中由pos位置开始截取len个长度的字符,len > str长度就结束 |
string (const char* s, size_t n) | 从s指向的字符数组中复制前n个字符 |
- 使用示例:
void Teststring1() { // 构造空的string类对象,等同于string s1(""); string s1; // 用C格式字符串构造string类对象s2 string s2("hello cole"); // 拷贝构造s3 string s3(s2); // 截取string类部分拷贝 string s4(s3, 6);//从第6个开始拷取到最后 // 截取字符数组部分拷贝 char str[] = "hello cole"; string s5(str, 5);//拷取前5个字符 cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; cout << s5 << endl; } // 注意:string类对象支持直接用cin和cout进行输入和输出
- 结果:
2、string类对象容量操作
- 使用示例1:
// size/clear/resize void Teststring2() { string s("hello cole!"); cout << s.size() << endl; cout << s.length() << endl; cout << s.capacity() << endl; cout << s << endl; // 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小 s.clear(); cout << s.size() << endl; cout << s.capacity() << endl; // 将s中有效字符个数增加到10个,多出位置用'a'进行填充 // “aaaaaaaaaa” s.resize(10, 'a'); cout << s.size() << endl; cout << s.capacity() << endl; // 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充 // "aaaaaaaaaa\0\0\0\0\0" // 注意此时s中有效字符个数已经增加到15个 s.resize(15); cout << s.size() << endl; cout << s.capacity() << endl; cout << s << endl; // 将s中有效字符个数缩小到5个 s.resize(5); cout << s.size() << endl; cout << s.capacity() << endl; cout << s << endl; }
- 使用示例2:
//reserve void Teststring4() { string s; // 测试reserve是否会改变string中有效元素个数 s.reserve(100); cout << s.size() << endl; cout << s.capacity() << endl; // 测试reserve参数小于string的底层空间大小时,是否会将空间缩小 s.reserve(50); cout << s.size() << endl; cout << s.capacity() << endl; }
- 使用示例3:
// 利用reserve提高插入数据的效率,避免增容带来的开销 void TestPushBack() { string s; size_t sz = s.capacity(); cout << "making s grow:\n"; for (int i = 0; i < 100; ++i) { s.push_back('c'); if (sz != s.capacity()) { sz = s.capacity(); cout << "capacity changed: " << sz << '\n'; } } } void TestPushBackReserve() { string s; s.reserve(100); size_t sz = s.capacity(); cout << "capacity: " << sz << '\n'; cout << "making s grow:\n"; for (int i = 0; i < 100; ++i) { s.push_back('c'); if (sz != s.capacity()) { sz = s.capacity(); cout << "capacity changed: " << sz << '\n'; } } }
注意:
size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()
clear()只是将string中有效字符清空,不改变底层空间大小
resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间
注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变
reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserve不会改变容量大小
3、string类对象访问及遍历操作
- 使用示例:
void Teststring5() { string s1("hello cole"); //使用operator[]+size()遍历字符串 for (size_t i = 0; i < s1.size(); i++) { //等同与s1.operator[](i) cout << s1[i] << " "; } cout << endl; //使用正向迭代器遍历字符串 string::iterator it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << " "; it1++; } cout << endl; //注意:iterator为普通迭代器,可读可写,const_iterator为const迭代器,只能读取字 string::const_iterator it3 = s1.begin(); while (it3 != s1.end()) { //*it3+=1; 会报错 cout << *it3 << " "; it3++; } cout << endl; //使用反向迭代器遍历字符串 string::reverse_iterator it2 = s1.rbegin(); while (it2 != s1.rend()) { cout << *it2 << " "; it2++; } cout << endl; //同样的反向迭代器的const迭代器为const_reverse_iterator //范围for遍历字符串 for (auto e : s1) { cout << e << " "; } }