【C++】C++标准模板库STL (一) string类的使用详解(2)

简介: 【C++】C++标准模板库STL (一) string类的使用详解(1)
  1. 我们再来看另一个扩容有关的函数:std::string::resize()函数

    此函数有两个版本,这两个版本构成函数重载。
    第一个参数是:调整后容量的大小,第二个参数是用什么字符来初始化新申请的空间中多余的没有被初始化部分空间,如果不给此参数,就默认用’\0’来初始化。
    如果第一个参数给的没有原来的大,那就是缩容,里面的字符串就变成了只保留原先字符串从0位置开始到n位置的字符串。
//std::string::resize()函数
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  cout << s1.size() << endl;
  cout << s1.capacity() << endl;
  s1.resize(30);  //提前把空间与字符数量扩容到50字节,但不初始化
          //由于string类内部的一些原因(如:对齐等原因),虽然向系统申请的的50字节,
          //但不一定就是50字节,总之系统申请的空间会 >= 你给的指定值
  cout << s1.size() << endl;
  cout << s1.capacity() << endl;
  return 0;
}

注意

  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 n=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。

3. string类对象的单个元素访问

//string 里面关于单个元素的访问
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world!");
  char ch1 = s1[1];   //operator[]的运算符重载
  char ch2 = s1[2];   //operator[]的运算符重载
  char ch3 = s1.at(6);
  cout << ch1 << endl;
  cout << ch2 << endl;
  cout << ch3 << endl;
  cout << "---------------" << endl;
  cout << s1.front() << endl;
  cout << s1.back() << endl;
  cout << "---------------" << endl;
  //捕获异常
  try
  {
    s1.at(100);
  }
  catch (const std::exception& e)
  {
    //显示异常原因
    cout << e.what() << endl;
  }
  return 0;
}


4. string类对象的元素的遍历

对于string类对象的遍历有以下四种方式:

  • 第一种operator运算符重载我们已经学会了,我们可以利用如下代码进行遍历字符串:
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  for (int i = 0; i < s1.size(); ++i)
  {
    cout << s1[i];
  }
  cout << endl;
  return 0;
}

  • 第二种beginend就需要借助STL里面的迭代器了,iterator是迭代器类型,是一种自定义类型,现在你可以暂时把它理解为类似于指针的类型。
    想要遍历字符串我们就应该这样遍历:
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  string::iterator it = s1.begin();
  while (it != s1.end())
  {
    cout << *it;
    ++it;
  }
  cout << endl;
  return 0;
}

  • 第三种是rbeginrend 它们其实也是迭代器,不同的是,它们是反向迭代器,所以我们应该这样写才能遍历string对象。
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  string::reverse_iterator it = s1.rbegin();
  while (it != s1.rend())
  {
    cout << *it;
    ++it;
  }
  cout << endl;
  return 0;
}

  • 第三种是范围for,在前面的文章中我们也谈论过范围for,利用范围for,也可以遍历string类(范围for的底层实现就是迭代器)。
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s1("hello world");
  for (auto& e : s1)
  {
    cout << e;
  }
  cout << endl;
  return 0;
}


在前面已经简单的介绍了两种迭代器,(正向迭代器与反向迭代器)下面我们就把另外两种迭代器进行简单介绍补充:

  • const_iterator类型的迭代器
    此类型的迭代器,不能通过迭代器来改变指向的对象里的内容。
#include<iostream>
#include<string>
using namespace std;
int main()
{
  const string s1("hello world");
  string::const_iterator it = s1.begin();
  while (it != s1.end())
  {
    (*it)++;  //此行为非法!
    it++;   //此行为合法!
  }
  return 0;
}
  • const_reverse_iterator类型的迭代器
    此类型的迭代器是反向迭代器,并且不能通过迭代器来改变指向的对象里的内容。
#include<iostream>
#include<string>
using namespace std;
int main()
{
  const string s1("hello world");
  string::const_reverse_iterator it = s1.rbegin();
  while (it != s1.rend())
  {
    (*it)++;  //此行为非法!
    it++;   //此行为合法!
  }
  return 0;
}

5.string类对象的修改操作

#include<iostream>
#include<string>
using namespace std;
int main() 
{
  string s0("hello ");
  string s1("world");
  s0 += s1;
  cout << s0 << endl;
  s0 += " string";
  cout << s0 << endl;
  s0 += '!';
  cout << s0 << endl;
  return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  string s1("world");
  //substring (2)
  s0.append(s1, 0, 3);
  cout << s0 << endl;
  //buffer (4)  
  s0.append("string", 3);
  cout << s0 << endl;
  //fill (5)
  s0.append(5, '!');
  cout << s0 << endl;
  return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  s0.push_back('!');
  cout << s0 << endl;
}

代码与append()函数代码类似,不再演示


注意:

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  string s1("world");
  //string (1)  
  s0.insert(0, s1);
  cout << s0 << endl;
  //c-string (3)  
  s0.insert(3, "!!!!!!");
  cout << s0 << endl;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  s0.erase(2, 2);
  cout << s0 << endl;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  //c-string (3)
  s0.replace(2, 2,"!!!!!");
  cout << s0 << endl;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello ");
  string s1("world");
  s0.swap(s1);
  cout << s0 << endl;
  cout << s1 << endl;
  return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s0("hello");
  s0.pop_back();
  cout << s0 << endl;
  return 0;
}


相关文章
|
1天前
|
机器学习/深度学习 JSON 编译器
C++ 资源大全:标准库、Web框架、人工智能等 | 最全整理
C++ 资源列表,内容包括: 标准库、Web应用框架、人工智能、数据库、图片处理、机器学习、日志、代码分析等
15 1
|
4天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1
|
4天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
10 0
|
4天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
14 0
【C++】string学习 — 手搓string类项目
|
4天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
4天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)
|
4天前
|
存储 编译器 C语言
【C++从练气到飞升】02---初识类与对象
【C++从练气到飞升】02---初识类与对象
|
5天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
16 0
|
6天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
17 1