C++STL中string详解(零基础/小白,字符串)

目录

1. 基本概念:

1.1 本质:

1.2 string和char*区别:

1.3 特点:

2. 构造函数(初始化)

3. 赋值操作

4. 字符串拼接

5 查找 和 替换

6. 字符串比较

7. 字符存取

8. 插入和删除

9. 子串获取


        下面讲解都是默认大家对STL有所了解进行讲解的,即知道容器,算法,迭代器的基本概念,如果你对这些还不太熟悉,可以阅读下面这篇文章

C++中STL的概念——零基础/小白向,适合竞赛,初学C++者使用-CSDN博客

        此外,下面处了会对string字符串的基本概念进行讲解,还有介绍其中的接口函数,当然你不需要去死记硬背,你只需要掌握其中1中即可。

1. 基本概念:

1.1 本质:

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

1.2 string和char*区别:

        char* 是一个指针。

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

1.3 特点:

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

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

        string管理char*所分配的空间,不用担心赋值越界和取值越界的问题,有类内部负责。

2. 构造函数(初始化)

        接下来都是先展示一下函数原型,具体如何操作将展示在代码中,如果看不懂函数原型,不要紧,直接看代码展示即可,学会怎么使用才是关键。当然要不需要去背,使用多了,自然记住了,并且记住的就是那些经常被使用的重要的。

初始化:

1. string();        //创建一个空的字符串

2. string(const char* s);            //使用字符串s初始化

3. string(const sting& str);        //使用一个string对象初始化另一个string对象

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

	// 第一种方法 创建空字符串
    string s1;	

	//第二种    使用字符串s初始化string对象
	const char* s = "hello world";
	string s2(s);
	cout << s2 << endl;

	//第三种    使用一个string对象初始化另一string对象
	string s3(s2);
	cout << s3 << endl;

	//第四种    将string初始化为n个字符c
	string s4(10, 'a');
	cout << s4 << endl;

        

3. 赋值操作

赋值操作 : 给string字符串进行赋值
        一类:
            string& operator = (const char* s);
            string& operatot = (const string& s);
            sting&    operator = (char c);

        
        二类:

            string& assign(const string& s);
            string& assign(const char* s);
            string& assign(const char* s,int n);
            string& assign(int n,char c);

//一类:
	//1.1
	string s1;
	s1 = "hello";
	cout << s1 << endl;

	//1.2
	string s2;
	s2 = s1;
	cout << s2 << endl;

	//1.3
	string s3;
	s3 = 'c';
	cout << s3 << endl;


//二类:
	//2.1
	string s4;
	s4.assign(s1);
	cout << s4 << endl;

	//2.2
	string s5;
	s5.assign("hello");
	cout << s5 << endl;

	//2.3
	string s6;
	s6.assign("hello", 3);
	cout << s6 << endl;

	//2.4
	string s7;
	s7.assign(5, 'h');
	cout << s7 << endl;

        其实初始化和赋值大部分都比较相似,熟悉了另一种就比较容易了。

        对于是否要全部掌握,= 和 assign函数,其实并不需要担心,目前你先只要熟练掌握其中一种就好了。

4. 字符串拼接

拼接操作 :  实现在字符串末尾拼接字符串
        一类:
        string& operator += (const char* s);
        string& operator += (const char c);
        string& operator += (const string& str);

        
        二类:
        string& append(const char* s);
        string& append(const char* s,int n);
        string& append(const string& str);
        string& append(const string& str,int pos,int n);

//第一类:
	//1.1
	string s1;
	s1 += "hello ";
	cout << s1 << endl;

	//1.2
	s1 += 'C';
	cout << s1 << endl;

	//1.3
	string s2 = " !";
	s1 += s2;
	cout << s1 << endl;

//第二类:
	//2.1
	string s3 = "hello  ";
	s3.append("world");
	cout << s3 << endl;

	//2.2
	s3.append(s2);
	cout << s3 << endl;

	//2.3 
	s3.append("and C++", 7);
	cout << s3 << endl;

	//2.4
	s3.append(s1, 0, 5);
	cout << s3 << endl;

5 查找 和 替换

查找:查找指定字符串是否存在
        ——find 和 rfind的区别 :find从左往右查找,rfind从右往左查
        int find(const string& str,int pos);    查找str第一次出现的位置,从pos位置开始查找
        int find(const char* s,int pos);    查找s第一次出现的位置,从pos位置开始查找
        int find(const char* s,int pos,int n);    从pos位置查找s的前n个字符第一次位置
        int find(const char c,int pos);    查找字符c第一次出现的位置,从pos位置开始

替换:在指定的位置替换字符串
        string& replace(int pos,int n,const string& str);
        string& replace(int pos,int n,const char* s);

        这里我们并不会对所有的接口函数进行实现,只要把最常用的和一些需要注意的介绍一下,其他的留给你自己来实验。

        1. find查找字符或字符串:

        如果想从0位置开始,即pos=0,是可以省略的。

string str = "hello"
int pos1 = str.find("hel");
int pos2 = str.find('o');

        2. find从pos位置开始查找s的前n个字符出现的第一个位置

string str("hello string !");
int pos1 = str.find("strnig",0,3);
int pos2 = str.find("strnig",0,6);
cout<<pos1<<endl;
cout<<pos2<<endl;

        这里pos1为s的位置,而pos2为-1,因为str中没有strnig这个字符串,其中根本原因是n的不用,strnig这个字符串只有前3个字符出现在str中。

        3. replace替换字符串

string str1 = "hello world";
str1.replace(6,3,"C++");

string str2 = "hello world";
str2.replace(6,5,"C++");

        这里替换就是将前n个元素替换成新的字符串,新的字符串不受n的影响,n只影响被替换的字符个数。当n=0时,在pos位置插入。

6. 字符串比较

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

= 返回 0

> 返回 1

< 返回 -1

int compare(const string& str) ;

int compare(const char* s);

	string str1 = "hello";
	string str2 = "world";
	int com1 = str2.compare(str1);
	cout << com1 << endl;

	int com2 = str1.compare(str2);
	cout << com2 << endl;

7. 字符存取

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

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

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

string str = "hello";
str[0] = 'w';
cout << str << endl;
str.at(0) = 'h';
cout << str << endl;

8. 插入和删除

插入:
string& insert(int pos,const char* s);        //插入字符串
string& insert(int pos,const string& str);        //插入字符串
string& insert(int pos,int n,char c);        //在指定位置插入n个字符c

删除:

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

    string str1 = "hello ";
	str1.insert(6, "world");
	cout << str1 << endl;

	string str2 = " !";
	str2.insert(0, str1);
	cout << str2 << endl;

	string str3;
	str3.insert(0,3, 'a');
	cout << str3 << endl;

	str3.erase(0, 1);
	cout << str3 << endl;

9. 子串获取

从字符串中获取想要的子串

string substr(int pos,int n);        //返回由pos开始的n个字符组成的字符串

	string str = "abcdef";

	string substr = str.substr(1, 3);

	cout << "substr = " << substr << endl;


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋刀鱼的滋味@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值