<C++>详解string容器,揭开string容器的神秘面纱

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: <C++>详解string容器,揭开string容器的神秘面纱

✨string容器


string基本概念


本质:


string是c++风格的字符串,不同于c语言的 char*,他本质是一个类

string 和 char*的区别:


char*是一个指针

string是一个类,类内部封装了char*来管理字符串,是一个char&型的容器

特点:


strint类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete,替换replace,插入insert

string管理char*所分配的内存,不用考虑赋值越界和取值越界等问题,由类内部进行负责


string构造函数

四种函数原型


string()创建一个空的字符串

string(const char* s)使用字符串s初始化

string(const string& str)使用一个string对象初始化另一个string对象

string(int n,char c)使用n个字符c初始化

使用示例:


//string的构造函数
void test1()
{
  string s1;//默认构造
  const char* str = "叶落 秋白";
  string s2(str);
  cout << "s2:"<<s2 << endl;
  string s3(s2);
  cout << "s3" << s3 << endl;
  string s4(6,'a');


上面就是四个构造方法对应的举例了,第一种方式是我们频繁使用的;第二种方式就是设置不可变字符数组传入构造来初始化;第三种方式理解为调用拷贝构造即可;第四种方式就比较有意思了,在上面代码里的意思就是用6个'a'来初始化字符串,输入s4结果为:aaaaaa。


string赋值操作

给string字符串赋值

赋值的函数原型:


string& operator = (const char* s)char*类型字符串 赋值给当前的字符串

string& operator = (const string &s)把字符串s赋给当前的字符串

string& operator = (char c)把字符赋值给当前的字符串

string& assign(const char* s)把字符串s赋值给当前的字符串

string& assign(const char*s,int n)把字符串s的当前n个字符赋给当前的字符串

string& assign(const string &s)把字符串s赋给当前字符串

string& assign(int n,char c)用n个字符c赋给当前字符串


使用示例:


void test2()
{
  string str1;
  str1 = "叶落秋白";
  cout << "str1=" << str1 << endl;
  string str2;
  str2 = str1;
  cout << "str2=" << str2 << endl;
  string str3;
  str3 = 'c';
  cout << "str3=" << str3 << endl;
  string str4;
  str4.assign("hello c++");
  cout << "str4=" << str4 << endl;
  string str5;
  str5.assign("hello c#",5);
  cout << "str5=" << str5 << endl;
  string str6;
  str6.assign(str5);
  cout << "str6=" << str6 << endl;
  string str7;
  str7.assign(6, 'w');
  cout << "str7=" << str7 << endl;
}


tips:stirng赋值方法很多,但是重载的operator=的方式最为常用


string拼接操作

在字符串末尾拼接字符串

函数原型:


string& operator+=(const char* str)重载+=操作符

string& operator+=(const char c)重载+=操作符

string& operator+=(const string& str)重载+=操作符

string& append(const char* s)把字符串s连接到当前字符串结尾

string& append(const char* s,int n)把字符串s的前n个字符连接到当前字符串的结尾

string& append(const string &s)同operator+=(const string& str)

string& append(const string &s,int pos,int n)把字符串s中从pos开始的n个字符连接到字符串结尾


使用示例:


void test3()
{
  string str1 = "红豆";
  str1 += "忆相思";
  cout << "str1=" << str1<< endl;
  str1 += '?';
  cout << "str1=" << str1 << endl;
  string str2 = "yyds";
  str1 += str2;
  cout << "str1=" << str1 << endl;
  string str3 = "You";
  str3.append("low");
  cout << "str3=" << str3 << endl;
  str3.append("wuwuwu qaq", 4);
  cout << "str3=" << str3 << endl;
  str3.append(str2);
  cout << "str3=" << str3 << endl;
  str3.append(str2, 0, 1);
  cout << "str3=" << str3 << endl;
}


tips:初学者只需要稍微记几个拼接函数即可


string查找替换

指定位置查找字符串

指定位置删除字符串

函数原型:


查找s第一次出现位置,从pos开始查找

int find(const string& str, int pos = 0) const;

int find(const char* s , int pos ==0) const;

从pos位置查找s的前n个字符第一次位置

int find( const char* s, int pos, int n) const;

查找字符c第一次出现位置

int find(const char c, int pos = e) const;

查找str最后一次位置,从pos开始查找

int rfind(const string& str, int pos = npos) const;

查找str最后一次位置,从pos开始查找,计数永远是从前往后

int rfind(const char* s, int pos = npos) const;

从pos查找s的前n个字符最后一次位置

int rfind(const char* s, int pos, int n) const;

查找字符c最后一次出现位置

int rfind(const char c, int pos - e) const;

替换从pos开始n个字符为字符串str

string& replace(int pos, int n, const string& str);

替换从pos开始的n个字符为字符串s

string& replace(int pos, int n,const char* s );

使用示例:


//字符串的查找和替换
//查找
void test4()
{
  string str1 = "abcdefgh";
  //找到返回下标,找不到返回-1
  int pos1 = str1.find("def");
  cout << "pos1=" << pos1 << endl;
  int pos2 = str1.find("s");
  cout << "pos2=" << pos2<< endl;
  pos1 = str1.rfind("ab");//从右往左找到第一个出现,从左往右计数
  cout << "pos1=" << pos1 << endl;;
}
//替换
void test5()
{
  string str2 = "abcdef";
  str2.replace(1, 2, "1111");//从1号位置起,2个字符替换为1111
  cout << "str2=" << str2 << endl;
}


tips:

find找到字符串后返回查找的第一个字符位置,找不到返回1

函数虽然很多,但几乎都是两个版本的,一个是c++风格一个c语言风格


string字符串比较

字符串比较是按字符的ASCII码进行对比

函数原型:


int compare(const string &s) const;

int compare(const char* s) const;

使用示例:


string str1 = “zello”;
string str2 = “hello”;
if (str1.compare(str2) == 0)
{
cout << “相等” << endl;
}
else if (str1.compare(str2) > 0)
{
cout << “str1大” << endl;
}
else
{
cout << “str2大” << endl;
}


tips:字符串对比的目的是比较两个字符串是否相等,判断谁大谁小的意义并不是很大。


string字符读取

单个字符存取有两种方式:

函数原型:


char& operator[] (int n); //通过[]获取字符

char& at (int n); //通过at方法获取字符

使用示例:


string str1 = “hello”;
//通过[]访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//通过at方式访问的单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改单个字符
str1[0] = ‘z’;
cout << str1 << endl;
str1.at(0) = ‘x’;
cout << str1 << endl;

string插入和删除

函数原型:


string& insert(int pos,const cahr* s);//在n位置插入字符串

string& insert(int pos,const string& s);//在n位置插入字符串

string& insert(int pos,int n,char c);//在指定位置插入n个字符c

string& erase(int pos,int n = npos);//删除从pos位置开始的n个字符

使用示例:


string str = “hello”;
//插入
str.insert(1, “111”);
cout << "str = " << str << endl;
//删除
str.erase(1,3);
cout << "str = " << str << endl;


tips:插入和删除的起始下标都是从0开始。


string求子串

从字符串中得到想要的子串

函数原型:


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


//string求子串
void test01()
{
  string str = "abcdef";
  string subStr = str.substr(1, 3);
  cout << "subStr=" << subStr << endl;
}
//使用操作
void test02()
{
  string email = "ylqb@qq.com";
  //从邮箱地址中获取用户名信息
  int pos = email.find("@");
  string usrName = email.substr(0, pos);
  cout << usrName << endl;
}


tips:灵活的运用求子串功能,可以在实际开发中获取有效的信息,在上述代码中就可以有效获取到不同邮箱中的用户名。

相关文章
|
18小时前
|
存储 C语言
从C语言到C++_13(string的模拟实现)深浅拷贝+传统/现代写法(中)
从C语言到C++_13(string的模拟实现)深浅拷贝+传统/现代写法
4 0
|
18小时前
|
存储 编译器 测试技术
从C语言到C++_13(string的模拟实现)深浅拷贝+传统/现代写法(上)
从C语言到C++_13(string的模拟实现)深浅拷贝+传统/现代写法
7 0
|
18小时前
|
存储 编译器 C语言
从C语言到C++_11(string类的常用函数)力扣58和415(中)
从C语言到C++_11(string类的常用函数)力扣58和415
5 0
|
18小时前
|
存储 C语言 C++
从C语言到C++_11(string类的常用函数)力扣58和415(上)
从C语言到C++_11(string类的常用函数)力扣58和415
5 0
|
4天前
|
存储 算法 搜索推荐
C++|STL简介-string-vector基础运用
C++|STL简介-string-vector基础运用
|
6天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
6天前
|
调度 C++ 容器
【C++】手搓 list 容器
本文我们实现了STL库中重要的list 的模拟实现,其中最重要莫过于迭代器的封装类的书写,这是前所未有的操作(对于我来说,我是第一次使用这种结构)。通过list 的模拟实现也帮我们巩固了类与对象的知识,也强化了指针操作的思路。欢迎大家讨论分析。
14 1
|
6天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
10 1
|
1天前
|
C++
【C++基础】类class
【C++基础】类class
9 1
|
1天前
|
安全 程序员 编译器
C++程序中的基类与派生类转换
C++程序中的基类与派生类转换
8 1