第九层(2):STL之string类

简介: 第九层(2):STL之string类

前情回顾


在上一章中,我上到了最后一层,了解到了什么是STL,并且学到一些基本的使用,当我以为就此结束的时候,发现第一个石碑倒下之后,后面还有一座石碑…


🚄上章地址:第九层(1):初识STL


string类


我看着面前的石碑,陷入沉思:难道第九层和下面的构造是不一样的吗?第九层后面还有很多石碑吗?只要这个石碑倒下就能验证猜想…


string类的本质


string是C++风格的字符串,而它本质上是一个类


string与char*的区别


char*是一个指针,可以指向一个字符串的起始地址

string是一个类,但类内本质上是封装了一个char*,用于管理字符串,是char*的一个容器


string类的特点


string类内部封装了很多成员函数,如:查找find、拷贝copy、删除delete、替换replace、插入insert

string管理char*所分配的内存,所以不用担心复制越界和取值越界的问题,由类内直接复制


string类的构造函数


string类中的构造函数有四个,有一个创建和三个初始化:

string();//创建一个空的字符串
string(const char* s);//使用字符串s对string对象进行初始化
string(const string& s);//使用一个string对象初始化另一个string对象
string(int n,char c);//使用n个字符c初始化string对象


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s;
  const char* c = "CPP";
  string s1(c);
  cout << s1 << endl;
  string s2(s1);
  cout << s2 << endl;
  string s3(5, 'C');
  cout << s3 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png

可以根据喜好选择初始化的方法。


string类内的字符串追加函数


对于字符串追加函数,在C++中同样存在,只不过封装在了在string类内部,并且有更多选择:

string& operator+=(const char* str);//操作符重载,可以将常量字符串str追加到string对象后面
string& operator+=(const char s);//操作符重载,将常量字符s追加到string对象后面
string& operator+=(const string& str);//操作符重载,将string对象str追加到另一个string对象后面
string& append(const char* str);//可以将常量字符串str追加到string对象后面
string& append(const char* str, int n);//可以将常量字符串str的前n个字符追加到string对象后面
string& append(const string& str);//将string对象str追加到另一个string对象后面
string& append(const string& str,int pos,int n)://将string对象str从pos下标开始的n个字符追加到另一个string对象后面


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  const char* str = "CPP";
  string s1; 
  s1 += str;
  cout << s1 << endl;
  const char s = '=';
  s1 += s;
  cout << s1 << endl;
  string s2;
  s2 += s1;
  cout << s2 << endl;
  string s3("I love");
  s3.append(str);
  cout << s3 << endl;
  string s4;
  s4.append(str, 2);
  cout << s4 << endl;
  string s5;
  s5.append(s3);
  cout << s5 << endl;
  s4.append(s3, 2, 4);
  cout << s4 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的字符串查找函数


在C语言中,有字符串查找的库函数,同样在C++中,封装到了string类当中:

int find(const string& str ,int pos = 0)const;//查找string对象str第一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos = 0)const;//查找常量字符串str第一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos,int n)const;//查找常量字符串str是否存在,从下标为pos的查找前n个字符
int find(const char s,int pos = 0)const;//查找常量字符s第一次出现的位置
int rfind(const string& str ,int pos = npos)const;//查找string对象str最后一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos = npos)const;//查找常量字符串str最后一次出现的位置,从下标为pos的开始查找
int find(const char* str, int pos,int n)const;//查找常量字符串str是否存在,从下标为pos的查找前n个字符
int find(const char s,int pos = npos)const;//查找常量字符s最后一次出现的位置


在C++中的查找和C的中的查找是一样的,找到返回下标,找不到返回-1,那为什么在C++中要出现find和rfind呢?它们的区别是什么?

find是从左往右查

rfind是从右往左查

使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("asdffdsadffdsa");
  string s1("ff");
  int f1 = s.find(s1);
  cout << f1 << endl;
  int f2 = s.find("ff");
  cout << f2 << endl;
  int f3 = s.find("ff", 5,6);
  cout << f3 << endl;
  int f4 = s.find('d');
  cout << f4 << endl;
  int f5 = s.rfind(s1);
  cout << f5 << endl;
  int f6 = s.rfind("ff");
  cout << f6 << endl;
  int f7 = s.rfind("ff", 0, 5);
  cout << f7 << endl;
  int f8 = s.rfind('a');
  cout << f8 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的字符串替换函数


在string类中,可以完成字符串的替换:

string& replace(int pos ,int n ,const string& str);//把从下标pos开始的n个字符串替换成string对象str
string&replace(int pos ,int n ,const char* str);//把从下标pos开始的n个字符串替换成字符串str


当替换的字符串中字符个数大于被替换的字符个数时,会怎么样?

使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("h1 world");
  s.replace(1, 1, "ello");
  cout << s << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png

是会将要替换进行的字符串全都替换进去,但是不会改变我们不让改变的地方。


string类内的字符串比较函数


在C++中,也将字符串比较封装在了string类内,与C一样,是比较下标一样的ASCII码值,等于返回0,小于返回-1,大于返回1

int compare(const string& str)const;//与string对象str相比较
int compare(const char* str)const;//与常量字符串str相比较


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("asdfg");
  int c1 = s.compare("asdfg");
  cout << c1 << endl;
  int c2 = s.compare("asdfr");
  cout << c2 << endl;
  int c3 = s.compare("asdfa");
  cout << c3 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的字符单个访问函数


在C++中,字符串中的字符也可以进行单个访问,可以修改:

char& operator[](int n);//操作符重载,可以访问到下标为n的字符
char& at(int n);//通过at访问下标为n的字符


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("hello world");
  for (int i = 0; i < s.size()/*size可以获取字符串场长度*/; i++)
  {
  cout << s[i] << " ";
  }
  cout << endl;
  for (int i = 0; i < s.size()/*size可以获取字符串场长度*/; i++)
  {
  cout << s.at(i) << " ";
  }
  cout << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的插入函数


与string类内的替换函数是基本一样的,但是不一样的是,它不会让原本的字符消失

string& insert(int pos,const char* str);//从下标pos的位置开始插入常量字符串str
string& insert(int pos,const string& str);//从下标pos的位置开始插入string对象str
string& insert(int pos,int n,const char s);//从下标pos的位置开始插入n个字符s常量字符串str


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("h world");
  s.insert(1, "ello");
  cout << s << endl;
  string s1("ab");
  s1.insert(1, 3, 'c');
  cout << s1 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的删除函数


它可以指定删除一个区间内的内容

string& erase(int pos,int n=npos//表示string结束的最后位置);//从下标为pos的字符开始删除n个字符


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("hello 11world");
  s.erase(6,2);
  cout << s << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


string类内的获取字串函数


在string类内可以获取区间内的字符

string& substr(int pos=0,int n=npos)const;//返回从下标pos开始的n个字符


使用:


#include<string>
#include<iostream>
using namespace std;
void test1()
{
  string s("hello world");
  string s1;
  int n = s.find(" ");//通过查找找到下标
  s1 = s.substr(0, n);
  cout << s1 << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


还是石碑?


随着对string有了解之后,第二座石碑也倒下了,映入我眼帘的还是一座石碑…


😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔

🚀专栏:C++爬塔日记

🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉


相关文章
|
13天前
|
安全 Java
String类-知识回顾①
这篇文章回顾了Java中String类的相关知识点,包括`==`操作符和`equals()`方法的区别、String类对象的不可变性及其好处、String常量池的概念,以及String对象的加法操作。文章通过代码示例详细解释了这些概念,并探讨了使用String常量池时的一些行为。
String类-知识回顾①
|
2天前
|
安全 C语言 C++
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
23 4
|
2天前
|
存储 编译器 程序员
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
24 2
|
2天前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(三)
【C++】C++ STL 探索:String的使用与理解
|
2天前
|
存储 编译器 C++
【C++】C++ STL 探索:String的使用与理解(二)
【C++】C++ STL 探索:String的使用与理解
|
2天前
|
编译器 C语言 C++
【C++】C++ STL 探索:String的使用与理解(一)
【C++】C++ STL 探索:String的使用与理解
|
18小时前
|
C语言 C++
C++番外篇——string类的实现
C++番外篇——string类的实现
9 0
|
18小时前
|
C++ 容器
C++入门7——string类的使用-2
C++入门7——string类的使用-2
10 0
|
18小时前
|
C语言 C++ 容器
C++入门7——string类的使用-1
C++入门7——string类的使用-1
11 0
|
16天前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
28 0
java基础(13)String类