C++STL标准模板库——String类

系统管理员 2022-11-10 16:05 475阅读 0赞

C++STL标准模板库——String类

  • 一、STL简介
    • 1.1 什么是STL?
    • 1.2 STL的六大组件
  • 二、string类
    • 2.1 为什么学习string?
    • 2.2 标准库中的string类
    • 2.3 string类对象的常见构造
    • 2.4 string类对象的容量操作
      • 总结
    • 2.5 string类对象的访问及遍历操作
      • <1> operator[]
      • <2> 迭代器
    • 2.6 string类对象的修改操作
      • <1> 在字符串后尾插字符
      • <2> 字符串查找
    • 2.7 string类的非成员函数

在这里插入图片描述

一、STL简介

1.1 什么是STL?

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分 ,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架

1.2 STL的六大组件

在这里插入图片描述

二、string类

2.1 为什么学习string?

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问 。

在OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数 。

2.2 标准库中的string类

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作
  3. string在底层实际是:basic_string模板类的别名,typedef basic_stringstring;
  4. 不能操作多字节或者变长字符的序列。

在使用string类时,必须包含#include头文件以及using namespace std;

2.3 string类对象的常见构造


























构造函数名称 功能说明
string()(重点) 构造空的string类对象,即空字符串
string(const char* s) (重点) 用C-string来构造string类对象
string(size_t n, char c) string类对象中包含n个字符c
string(const string&s) (重点) 拷贝构造函数

在这里插入图片描述

2.4 string类对象的容量操作






































函数名称 功能说明
size(重点) 构造空的string类对象,即空字符串
length 用C-string来构造string类对象
capacity string类对象中包含n个字符c
empty(重点) 拷贝构造函数
clear(重点) 清空有效字符
reserve(重点) 为字符串预留空间
resize(重点) 将有效字符的个数改成n个,多出的空间用字符c填充
  1. void TestString1()
  2. {
  3. string s("hello");
  4. cout << s.size() << endl; //返回字符串中的有效字符,不包含'\0'
  5. cout << s.length() << endl; //等同于size,但是通常不用length求有效字符
  6. cout << s.capacity() << endl; //返回空间总大小,同样不包含'\0'
  7. cout << s << endl; //string类对象支持cin输入和cout打印
  8. // 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
  9. s.clear();
  10. cout << s.size() << endl;
  11. cout << s.capacity() << endl;
  12. // 将s中有效字符个数增加到10个,多出位置用'a'进行填充
  13. // “aaaaaaaaaa”
  14. s.resize(10, 'a');
  15. cout << s.size() << endl;
  16. cout << s.capacity() << endl;
  17. // 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
  18. // "aaaaaaaaaa\0\0\0\0\0"
  19. // 注意此时s中有效字符个数已经增加到15个
  20. s.resize(15);
  21. cout << s.size() << endl;
  22. cout << s.capacity() << endl;
  23. cout << s << endl;
  24. // 将s中有效字符个数缩小到5个
  25. s.resize(5);
  26. cout << s.size() << endl;
  27. cout << s.capacity() << endl;
  28. cout << s << endl;
  29. }

在这里插入图片描述

假设不指定空间大小则每次都会扩容,带来计算开销

  1. // 利用reserve提高插入数据的效率,避免增容带来的开销
  2. void TestPushBack()
  3. {
  4. string s;
  5. size_t sz = s.capacity();
  6. cout << s.capacity() << endl;
  7. cout << "making s grow:\n";
  8. for (int i = 0; i < 100; ++i)
  9. {
  10. s.push_back('c');
  11. //查看扩容了几次
  12. if (sz != s.capacity())
  13. {
  14. sz = s.capacity();
  15. cout << "capacity changed: " << sz << '\n';
  16. }
  17. }
  18. }

在这里插入图片描述

reserve的应用场景是为了解决string的增容缺陷,假设我们已经知道要开辟多大空间时,可以直接指定空间大小,避免增容带来的开销

  1. // 利用reserve提高插入数据的效率,避免增容带来的开销
  2. void TestPushBackReserve()
  3. {
  4. string s;
  5. s.reserve(100);
  6. size_t sz = s.capacity();
  7. cout << s.capacity() << endl;
  8. cout << "making s grow:\n";
  9. for (int i = 0; i < 100; ++i)
  10. {
  11. s.push_back('c');
  12. //查看扩容了几次
  13. if (sz != s.capacity())
  14. {
  15. sz = s.capacity();
  16. cout << "capacity changed: " << sz << '\n';
  17. }
  18. }
  19. }

在这里插入图片描述

使用reserve注意以下问题

  1. void TestString3()
  2. {
  3. string s;
  4. // 测试reserve是否会改变string中有效元素个数
  5. s.reserve(100);
  6. cout << s.size() << endl;
  7. cout << s.capacity() << endl;
  8. // 测试reserve参数小于string的底层空间大小时,是否会将空间缩小
  9. s.reserve(50);
  10. cout << s.size() << endl;
  11. cout << s.capacity() << endl;
  12. }

在这里插入图片描述

总结

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  2. clear()只是将string中有效字符清空,不改变底层空间大小。
  3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字
    符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的
    元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大
    小,如果是将元素个数减少,底层空间总大小不变。
  4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于
    string的底层空间总大小时,reserver不会改变容量大小。
  5. reserve和resize的区别是,reserve只增容不改变有效元素个数,resize即改变容量空间又对容量空间初始化。

2.5 string类对象的访问及遍历操作


























函数名称 功能说明
operator[] 返回pos位置的字符,const string类对象调用
begin+ end begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rend begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭 代器
范围for C++11支持更简洁的范围for的新遍历方式

<1> operator[]

  1. void TestStringOperator1()
  2. {
  3. string s1("hello world");
  4. cout << s1 << endl;
  5. s1[0] = 'H';
  6. cout << s1 << endl;
  7. const string s2("hello c++");
  8. // s2[0] = 'h'; 代码编译失败,因为const类型对象不能修改
  9. }

在这里插入图片描述

<2> 迭代器

对于迭代器,我们暂时可以将其看成指针

  1. //迭代器
  2. void TestStringIterator()
  3. {
  4. string s("hello world");
  5. //三种遍历方式:
  6. //以下三种方式除遍历string外还可修改string中的字符
  7. //第一种使用较多
  8. //1.for+operator[]
  9. cout << "for+operator[]" << endl;
  10. for (size_t i = 0; i < s.size() - 1; ++i)
  11. {
  12. cout << s[i] << " "<< endl;
  13. }
  14. //2.正向迭代器
  15. cout << "2.正向迭代器" << endl;
  16. string::iterator it = s.begin();
  17. while (it != s.end())
  18. {
  19. cout << *it << endl;
  20. ++it;
  21. }
  22. //2.反向迭代器
  23. cout << "2.反向迭代器"<<endl;
  24. string::reverse_iterator rit = s.rbegin();
  25. while (rit != s.rend())
  26. {
  27. cout << *rit << endl;
  28. ++rit;
  29. }
  30. //3.范围for
  31. cout << "3.范围for" << endl;
  32. for (auto ch : s)
  33. {
  34. cout << ch << endl;
  35. }

在这里插入图片描述

2.6 string类对象的修改操作






































函数名称 功能说明
push_back 在字符串后尾插字符c
append 在字符串后追加一个字符串
operator+= (重点) 在字符串后追加字符串str
c_str(重点) 返回C格式字符串
find + npos(重点) 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr 在str中从pos位置开始,截取n个字符,然后将其返回

<1> 在字符串后尾插字符

在这里插入图片描述

<2> 字符串查找

  1. //找文件尾
  2. void TestStringFind()
  3. {
  4. string s = "hello world";
  5. s.push_back('!'); //字符串后尾插字符
  6. cout << s << endl;
  7. s.append("HELLO"); //字符串后追加字符串
  8. cout << s << endl;
  9. s += 'W'; //+=追加字符
  10. cout << s << endl;
  11. s += "ORLD"; //+=追加字符串
  12. cout << s << endl;
  13. s += s; //追加string对象
  14. cout << s << endl;
  15. string file1("string.cpp");
  16. string file4("string.c.tar.zip");
  17. //substr第二个参数可以不写,从前往后找
  18. size_t pos = file1.find('.');
  19. if (pos != string::npos)
  20. {
  21. cout << file1.substr(pos, file1.size() - pos) << endl;
  22. }
  23. //rfind从后往前找
  24. size_t pos4 = file4.rfind('.');
  25. if (pos4 != string::npos)
  26. {
  27. cout << file4.substr(pos4) << endl;
  28. }
  29. //分割域名
  30. string url = "http://cplusplus.com/reference/string/string/find/";
  31. cout << url << endl;
  32. size_t pos5 = url.find(':'); //第一个冒号分割http
  33. if (pos5 != string::npos)
  34. {
  35. cout << url.substr(0, pos5) << endl;
  36. }
  37. size_t pos6 = url.find('/', pos5 + 3); //pos5+3:c pos6:/
  38. if (pos6 != string::npos) //分割网址cplusplus.com
  39. {
  40. cout << url.substr(pos5 + 3, pos6 - (pos5 + 3)) << endl;
  41. }
  42. cout << url.substr(pos6 + 1, string::npos) << endl;
  43. //删除http
  44. string copy = url;
  45. cout << copy.erase(0, pos5 + 3) << endl;
  46. }

在这里插入图片描述

在这里插入图片描述

注意:

  1. 在string尾部追加字符时,s.push_back© / s.append(1, c) / s += ‘c’三种的实现方式差不多,一般
    情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
  2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。

2.7 string类的非成员函数






























函数 功能说明
operator+ 尽量少用,因为传值返回,导致深拷贝效率低
operator>> (重点) 输入运算符重载
operator<< (重点) 输出运算符重载
getline (重点) 获取一行字符串
relational operators (重点) 大小比较

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,475人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++标准标准模板

      C++强大的功能来源于其丰富的类库及库函数资源。C++标准库的内容总共在50个标准头文件中定义。在C++开发中,要尽可能地利用标准库完成。这样做的直接好处包括:(1)成本: