C++学习_初阶(九)——string

1. 为什么要学习string类

1.1 C语言中的字符串

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP(面向对象)的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

1.2 面试题中

在OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数。

2. 标准库中的string类

C++库中的string类

  1. 字符串是表示字符序列的类
  2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
  3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅basic_string)。
  4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits 和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
  5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。

总结:

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
  3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>
    string;
  4. 不能操作多字节或者变长字符的序列。
    在使用string类时,必须包含#include头文件以及using namespace std;

2.2 string类的常用接口说明

2.2.1 string类对象的常见构造

(constructor)函数名称功能说明
string() (重点)构造空的string类对象,即空字符串
string(const char* s) (重点)用C-string来构造string类对象
string(size_t n, char c)string类对象中包含n个字符c
string(const string&s) (重点)拷贝构造函数
  1. string() :构造空的string类对象,即空字符串
	string s1;
	cout << "s1 =  " << endl;
  1. string(const char* s) :用C-string来构造string类对象
    //两种方式都可以
	string s2("Hello String~");
	cout << "s2 = " <<s2<< endl;

	string s3 = "Hello C++~";
	cout << "s3 = " << s3 << endl;
  1. string(size_t n, char c) :string类对象中包含n个字符c
	string s4(5,'y');
	cout << "s4 = " << s4 << endl;
  1. string(const string&s) :拷贝构造函数

	string s4(5,'y');
	cout << "s4 = " << s4 << endl;

	string s5(s4);
	cout << "s5 = " << s5 << endl;
  1. string (const string& str, size_t pos, size_t len = npos);
string s2("Hello String~");
cout << "s2 = " <<s2<< endl;

string s6(s2, 6, 7);
cout << "s6 = " << s6 << endl;

结果展示:
在这里插入图片描述
注意: 字符串仍然是以\0结尾的,所以如果我们在字符串中间出现\0,打印的时候就到\0停止,后面的字符会被截断。
代码演示

#include<iostream>
#include<string>
using namespace std;


void main()
{

	string s1("hello\0 String~");
	cout << "s1 =  " <<s1<< endl;


}

结果:
在这里插入图片描述

2.2.2 string类对象的容量操作

(constructor)函数名称功能说明
size(重点)返回字符串有效字符长度
length返回字符串有效字符长度
capacity返回空间总大小
empty (重点)检测字符串释放为空串,是返回true,否则返回false
clear (重点)清空有效字符
reserve (重点)为字符串预留空间**
resize (重点)将有效字符的个数该成n个,多出的空间用字符c填充
  1. size()
	string s1("hello string~");
	cout << "s1 =  " <<s1<< endl;
	cout << "size = " << s1.size() << endl;

在这里插入图片描述
2. 代码演示

#include<iostream>
#include<string>
using namespace std;


void main()
{
	

	string s1("hello string~");
	cout << "s1 =  " <<s1<< endl;
	cout << "size = " << s1.size() << endl;//STL 需要
	cout << "length = " << s1.length() << endl;//和size底层逻辑一模一样的。

	cout << "capacity = " << s1.capacity() << endl;

	string s2;
	s1.clear();
	cout <<"s1 = "<< s1 << endl;
	
}

在这里插入图片描述
3. capacity()

#include<iostream>
#include<string>
using namespace std;

//capacity——底层会自己根据字符串的长度进行扩建。但是绝不是一次只扩建一个,而是接近二倍来扩充的。
void main()
{
	string s1("hello string~");
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;
	s1 += "abcxyz";
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;
}

在这里插入图片描述

  1. reserve() :为字符串预留空间**
    需要大的空间,会继续扩容,但是扩大之后,需要小的空间,可能不会把刚刚扩容的空间给你释放掉。只要满足你的条件即可。(不同的编译器有不同的自己的方法)
    注意这里不是反转昂~是预留,反转是reverse
    代码示例:
#include<iostream>
#include<string>
using namespace std;


void main()
{
	string s1;
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;

	s1.reserve(100);
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;
}

结果
在这里插入图片描述

  1. resize():将有效字符的个数该成n个,多出的空间用字符c填充
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;


void main()
{
	string s1;
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;

	s1.resize(10,'@');
	cout << "s1 =  " << s1 << endl;
	cout << "size = " << s1.size() << endl;
	cout << "capacity = " << s1.capacity() << endl;
}

在这里插入图片描述

注意:

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  2. clear()只是将string中有效字符清空,不改变底层空间大小。
  3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
  4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。

2.2.3 string类对象的访问及遍历操作

(constructor)函数名称功能说明
operator[] (重点)返回pos位置的字符,const string类对象调用
begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rendbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围forC++11支持更简洁的范围for的新遍历方式
  1. 访问数据的四种方式。
#include<iostream>
#include<string>
using namespace std;

void main()
{
    //1
	string s1("abcdefghijklmnopqrstovwxyz");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;

    //2
	cout << s1 << endl;
    //3
	for (const auto& e : s1)
		cout << e;
	cout << endl;

   //4 这里得到的只是一个字符,而不是字符串。
   for (int i = 0; i < s1.size(); ++i)
		cout << s1[i];
	cout << endl;
}

2.2.4 string类对象的修改操作

(constructor)函数名称功能说明
push_back在字符串后尾插字符c
append在字符串后追加一个字符串
operator+= (重点)在字符串后追加字符串str
c_str(重点)返回C格式字符串、
find + npos(重点)从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr在str中从pos位置开始,截取n个字符,然后将其返回
  1. push_back:在字符串后尾插字符c
#include<iostream>
#include<string>
using namespace std;


void main()
{

	string s("abc");
	cout << "s = " << s << endl;
	s.push_back('x');//注意这里插入的是字符(单引号)不是字符串(双引号)!!
	cout << "s = " << s << endl;

}

在这里插入图片描述

  1. append:在字符串后追加一个字符串
#include<iostream>
#include<string>
using namespace std;

void main()
{
	string s("abc");
	cout << "s = " << s << endl;
	s.push_back('x');
	cout << "s = " << s << endl;
	s.append("ayz",2);
	cout << "s = " << s << endl;

	string s1("syzabc");
	s1.append(s1,3,3);
	cout << "s = " << s1 << endl;

	string s2("syzabc");
	s2.append(3,'x');
	cout << "s = " << s2 << endl;

}
  1. operator+= (重点) :在字符串后追加字符串str
#include<iostream>
#include<string>
using namespace std;


void main()
{

	string s("abc");
	string s1("xyz");
	s +=s1;
	cout << "s = " << s << endl;

}

在这里插入图片描述

  1. c_str(重点) :返回C格式字符串
    想要用C的函数求长度,就需要先把对象转化为C格式的字符串。
#include<iostream>
#include<string>
using namespace std;



void main()
{

	string s("abc");
	cout << s.size() << endl;

	strlen(s.c_str());
}

在这里插入图片描述

  1. find + npos(重点) :从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置(返回的不是地址是下标)
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;


void main()

{//注意这里不要简单粗暴的用 -1来判断,而是用npos
	//npos是静态的,要用类名来限定~
	string s = "axbcxyzx";

	//int index = s.find('x',2);//会返回最前面找到的符合字符的下标
	int index = s.find('w', 2);//没有找到会返回-1


	if (index == string::npos)//npos是 null pos  的简写。表示空位置。(str2ulong这里的2 是To 的意思)
		cout << "找不到" << endl;
	cout << "index = " << index << endl;


}

在这里插入图片描述

  1. rfind():从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置:
	int index = s.rfind('x');

	if (index == string::npos)//npos是 null pos  的简写。表示空位置。(str2ulong这里的2 是To 的意思)
		cout << "找不到" << endl;
	else
	cout << "index = " << index << endl;
  1. substr():在str中从pos位置开始,截取n个字符,然后将其返回。并不会改变原本的字符串。
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;



void main()

{
	string s = "abcxyz";
	cout << "s = " << s << endl;
	string s1 = s.substr(2,3);
	cout << "s1 =" << s1 << endl;


}

在这里插入图片描述
注意:

  1. 在string尾部追加字符时,s.push_back© / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
  2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。

2.2.5 string类非成员函数

(constructor)函数名称功能说明
operator+尽量少用,因为传值返回,导致深拷贝效率低
operator>> (重点)输入运算符重载
operator<< (重点)输出运算符重载
getline (重点)获取一行字符串
relational operators (重点)大小比较
  1. operator+:
#include<iostream>
#include<string>
using namespace std;
void main()
{

	string s1 = "asg";
	string s2 = "dmn";

	string s3 = s1 + s2;
	cout << "s3 = " << s3 << endl;
}
  1. 注意OJ题目中经常遇见
    输入字符串(1)cin (字符串中不能有空格)
    在这里插入图片描述
    输入字符串(2)getline
#include<iostream>
#include<string>
using namespace std;



void main()
{
	string s;
	//cin >> s;//这种形式输入 字符串不能包含空格,它以空格结尾的。
	getline(cin ,s);
	cout << s << endl;
}

在这里插入图片描述

  1. assign()
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;



void main()
{
	string s1 = "abc";
	s1.assign("xyzddd");
	cout << "s1 = " << s1 <<endl;
	//string &rs = s1.assign("xyzpljj");//引用接收,引用表示就是s1本身
	//cout << "s1 = " << s1 << endl;
	//cout << " rs = " << rs << endl;

	string s2 = "xyz";
	//s1.assign(s2);
	s2 = s1;
	cout << "s1 = " << s1 << endl;
	cout << " s2 = " << s2 << endl;
}

在这里插入图片描述
4. at ()
在这里插入图片描述
例子(1)

#include<iostream>
#include<string>
using namespace std;


void main()
{
	string s1 = "abc";
	for (int i = 0; i < s1.size(); ++i)
		cout << s1[i];//不会检查是否有越界情况。
	cout << endl;

	for (int i = 0; i < s1.size(); ++i)
		cout << s1.at(i);//会检查越界
	cout << endl;
}

在这里插入图片描述
例子(2)

#include<iostream>
#include<string>
using namespace std;



void main()
{
	string s1 = "abc";
	//s1[1] = 'B';
	s1.at(1) = 'B';
	cout << s1 << endl;


}

在这里插入图片描述
注意:这里为什么可以直接修改是因为 [ ]和at() 都是以引用返回的!

  1. compare()
    在这里插入图片描述
  2. copy
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;


void main()
{
	string s1 = "abc";
	char str[] = "yxyzm";
	s1.copy(str, 3, 0);
	cout << s1 << endl;
}
  1. insert()
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;


void main()
{
	string s1 = "abc";
	s1.insert(0, "xyz");
	cout << s1 << endl;
}

在这里插入图片描述

  1. erase()
    在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;



void main()
{
	string s1 = "abcyxyzm";
	s1.erase(2, 5);//从下标2位置删除5个元素
	//s1.erase(s1.begin(), s1.end());
	cout << s1 << endl;

}

在这里插入图片描述

3. string类的模拟实现

3.2 浅拷贝

浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为还有效,所以 当继续对资源进项操作时,就会发生发生了访问违规。要解决浅拷贝问题,C++中引入了深拷贝。

3.3 深拷贝

如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值