数据结构和算法(02)---字符串(c++)

目录

  • 数据结构:
    • 逻辑结构:数组,栈,队列,字符串,树,图
    • 存储结构:顺序存储,链式存储
  • C++常用的数据结构有:string , stack , queue , deque , vector , list , map , iterators.

在这里插入图片描述
参考博客

注意

可以将字符串看成一种特殊的顺序表,特殊之处在于表的节点只能存储字符串。由于字符串相邻元素的位置是由关系的,即顺序关系,不建议用链式存储结构存储

一.c风格的字符串与操作函数

1.c风格字符串

C风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。
字符串实际上是使用 null 字符 ‘\0’ 终止的一维字符数组。
因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

下面的声明和初始化创建了一个 “Hello” 字符串。
由于在数组的末尾存储了空字符,所以字符数组的大小比单词 “Hello” 的字符数多一个。

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

//依据数组初始化规则,您可以把上面的语句写成以下语句:

char greeting[] = "Hello";

以下是 C/C++ 中定义的字符串的内存表示:

在这里插入图片描述
其实,您不需要把 null 字符放在字符串常量的末尾。
C++ 编译器会在初始化数组时,自动把 ‘\0’ 放在字符串的末尾

让我们尝试输出上面的字符串:

#include <iostream>
using namespace std;
int main (){
    char s1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    char s2[6] = {'H', 'e', 'l', 'l', 'o'};
    char s3[] = "hello"; //编译器会根据字符串长度的大小初始化字符数组的

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
}

Hello
Hello
hello

2.c风格字符串处理函数

C++ 中有大量的函数用来操作以 null 结尾的字符串
在这里插入图片描述
下面的实例使用了上述的一些函数:

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
    char str1[11] = "Hello";
    char str2[11] = "World";
    char str3[11];
    int  len ;

    // 复制 str1 到 str3
    strcpy( str3, str1);
    cout << "strcpy( str3, str1) : " << str3 << endl;

    // 比较 str1 和 str2的大小
    cout<< strcmp(str1, str2) <<endl;  
    if(strcmp(str1, str2)>0)
        cout<<"str1 > str2"<<endl;
    else if(strcmp(str1, str2)<0)
        cout<<"str1 < str2"<<endl;
    else
        cout<<"str1 = str2"<<endl;

    // 连接 str2 到 str1
    strcat( str1, str2);
    cout << "strcat( str1, str2): " << str1 << endl;

    // 连接后,str1 的总长度
    len = strlen(str1);    //strlen计算字符串长度的时候,没有计算尾部 '\0'元素
    cout << "strlen(str1) : " << len << endl;

    // 返回一个指针,指向字符串 str1 中字符 ch 的第一次出现的位置
    char *p;
    char ch = 'e';
    p = strchr(str1, ch);
    cout<<*(p+3)<<endl;

    // 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置
    char *p2;
    char s2[] = "ll";
    p2 = strstr(str1, s2);
    cout<<*(p2+2)<<endl;
}

在这里插入图片描述


二.c++中的字符串与操作函数

1.c++中的string类

C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。我们将学习 C++ 标准库中的这个类,现在让我们先来看看下面这个实例:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string str1 = "Hello";
   string str2 = "World";
   string str3;
   int  len ;

   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;
}

str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10

2.string类的基本操作

string类提供了一系列针对字符串的操作:

  1. append() – 在字符串的末尾添加字符
  2. find() – 在字符串中查找字符串
  3. insert() – 插入字符
  4. length() – 返回字符串的长度
  5. replace() – 替换字符串
  6. substr() – 返回某个子字符串
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main(){
	//定义一个字符串
	string str1 = "hello word!!";
	cout<<str1<<endl;
	//获取字符串的长度
	cout<<"the len of str1 is:"<<str1.size()<<endl;
	//连接字符串
	string tempStr = " It`is a string example!!";
	cout<< (str1 + tempStr)<<endl;
	cout<<"the len of string is:"<<(str1 + tempStr).size()<<endl;
	//复制字符串
	string str2;
	str2 = str1;
	cout<<"str2 is:"<<str2<<endl;
	//在字符串末尾添加字符串
	cout<<"append str:"<<str1.append(" It`s a demo!!!")<<endl;
	// 查找子串的位置
	int pos = str1.find("llo");
	cout<<"the pos of llo is:"<<pos<<endl;
	//替换某些字符,使用空格替换掉上面查找的3个字符 
	str1.replace(pos,3," "); 
	cout<<"str1 is :"<<str1<<endl;
	//查找字符串中某个字符的第一次出现的位置和最后一次出现的位置
	str1 = "hello world!!!";
	int startPos = str1.find_first_of('l');
	int endPos = str1.find_last_of('l');
	cout<<"the fist pos is:"<<startPos<<"\t"<<"the last pos is:"<<endPos<<endl;
	//提取某个子串
	cout<<str1.substr(startPos+1,endPos-startPos + 1)<<endl;
	
	return 0;
}

运行程序输出:
hello word!!
the len of str1 is:12
hello word!! Itis a string example!! the len of string is:37 str2 is:hello word!! append str:hello word!! Its a demo!!!
the pos of llo is:2
str1 is :he word!! It`s a demo!!!
the fist pos is:2 the last pos is:9
lo world

3.string类的操作汇总

字符串操作函数
这里是C++字符串的重点,把各种操作函数罗列出来:
a) =,assign() //赋以新值
b) swap() //交换两个字符串的内容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //删除字符
f) clear() //删除全部字符
g) replace() //替换字符
h) + //串联字符串
i) ==,!=,<,<=,>,>=,compare() //比较字符串
j) size(),length() //返回字符数量
k) max_size() //返回字符的可能最大个数
l) empty() //判断字符串是否为空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量内存以容纳一定数量的字符
o) [ ], at() //存取单一字符
p) >>, getline() //从stream读取某值
q) << //将谋值写入stream
r) copy() //将某值赋值为一个C_string
s) c_str() //将内容以C_string返回
t) data() //将内容以字符数组形式返回
u) substr() //返回某个子字符串
v)查找函数
w)begin() end() //提供类似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置器

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值