数组矩阵广义表:C++中string字符串类型介绍

2022-07-1713:19:49数据结构与算法Comments1,025 views字数 1579阅读模式

1. 简介文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

C语言中通过字符相连已经基本创造出了字符串的常规操作,然而,字符串在C语言中并不是常规类型,而是一个类似于数组的结构,在C++中,通过模板类的操作创建了string类,这样的方法更适合于现代的操作编程。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

2. 头文件&命名空间文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

#include<string>     //头文件
注意不是<string.h>也不是<cstring>,前者为C文件中的头文件,后者为C++中调用的C文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

using namespace std; //命名空间
string来自于std命名空间,常用的绝大多数操作均来自这个空间,string也不例外。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

3. 初始化文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

与第五章的STL内容十分相近,两者的创建方法基本完全相等,这里直接使用代码举例,不多做缀数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

#include<iostream>
#include<string>
using namespace std;
int main(){
string str;       //创建空字符串文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str1 = "Hello IDCnote";
cout<<str1<<endl;   //Hello IDCnote文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str2(str1);
cout<<str2<<endl;   //Hello IDCnote文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str3(str1,6);
cout<<str3<<endl;   //IDCnote文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str4(str1,6,6);
cout<<str4<<endl;   //Dotcpp文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str5(5,'D');
cout<<str5<<endl;   //DDDDD文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str6(str1.begin(),str1.end());
cout<<str6<<endl;   //Hello IDCnote
return 0;
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

4. 比较与连接操作操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

对于比较类型的操作,可以使用 ==,>,<,>=,<=与!=进行比较字符串,对于链接操作而言,可以直接使用符号+进行直接的操作符连接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

比较:对于C语言的字符串操作而言,我们通过使用strcmp函数进行比较,通过这个函数的返回值判断两字符串的大小情况,这样的比较方式确实已经满足了很多功能,但是还不如我们常规的思路,C++中利用符号重载的思路重构了比较方法,直接通过str1==str2的以及相关的方式进行比较,这比较符合我们大多数情况的人类思维,如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str1 = "Hello";
string str2 = "Hello";
if(str1==str2)
cout<<"两者相等"<<endl;
else
cout<<"两者不相等"<<endl;
对于字符串的连接操作,对于C语言而言需要使用字符串连接函数,同理,C++使用符号重载直接让我们通过‘+’号完成操作,这也更符合我们的思维,避免了麻烦。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string totalystring = "a";
cout<<totalystring+ "b"+"c"+"d"<<endl;
5.  常用操作(节选)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

a) 获取长度length文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

获取当前字符串的长度,这与C中的strlen(str)一致。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

str.length();
b)获取大小size文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

获取当前字符串的大小,某种意义上由于字符串的每一个字符开辟的空间均完全相等,因此size可以代替length文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

str.size();
c) 判断是否为空empty文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

返回一个布尔类型,判断字符串是否为空empty。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

str.empty();
d) 填充内容resize文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

原型为void resize(int len,char chr)把字符串当前大小置为len,多去少补,多出的字符chr填充不足的部分,resize将会修改该字符串的占用空间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

string str = "HEllo";
str.resize(8,'K');
cout<<str<<endl;
输出文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

HElloKKK文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/25132.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/suanfa/25132.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定