C++ string类

目录

为什么要有string类?

auto和范围for

1. auto

2. 范围for

一. 标准库中string类的简单使用

二. string类的常用接口说明

1. string类对象的常见构造

2. string类对象的容量操作

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

4. string类对象的修改与查找操作

 插入字符或字符串​编辑

字符串替换

字符串的交换

查找的使用

5. string类非成员函数


为什么要有string类?

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

auto和范围for

C++11的两个小语法

1. auto
  •  在早期C/C++中auto的含义是:: 使用auto修饰的变量,是具有自动存储器的局部变量,后来这个不重要了。C++11中,标准委员会变废为宝赋予了 auto全新的含义,即:auto不再是一个存储类型的指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得
  • auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a = 100;
	auto b = a;
	auto c = 'c';
	//auto e;//报错,必须具有初始值设定

	cout << typeid(a).name() << endl;
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;

	auto pa1 = &a;
	auto* pa2 = &a;

	auto& pa3 = a;//声明引用类型必须加&
	cout << a << endl;
	pa3 = 1;
	cout << typeid(pa1).name() << endl;
	cout << typeid(pa2).name() << endl;
	cout << pa3 << "  " << a<<endl;


	return 0;
}
  • 当在同一行声明多个变量时这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后再用推导出来的类型定义其他变量
  • auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
  • auto不能直接用来声明数组

如下代码

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

void fun1(auto a)
{
	return a;
}

int main()
{
	auto a = 10, b = 11.11;
	auto c = 'x', d = 1.01;

	auto aa[] = { 1,2,3,4 };

	return 0;
}

报错分别为

  指在同一行第一个类型推导与后不一致

此处是指函数参数处

即auto不能直接声明数组类型

2. 范围for
  • 对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此C++11中引入了基于范围的for循环。for循环后的括号由冒号" : " 分为两部分:第一部分是范围内用于迭代的变量,第二部分则表示被迭代的范围自动迭代,自动取数据,自动判断结束
  • 范围for可以作用到数组和容器对象上进行遍历。
  • 范围for的底层很简单,容器遍历实际就是替换为迭代器,这个从汇编层也可以看到。
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int arr[] = { 1,2,3,74,6 };

	for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
	{
		arr[i] *= 2;
	}
	for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
	{
		cout << arr[i] << "  ";
	}
	cout << endl;

	for (auto v : arr)
	{
		v *= 2;
	}
	for (auto v : arr)
	{
		cout << v << "  ";
	}
	cout << endl;

	string s1 = "hello world";
	for (auto s : s1)
	{
		cout << s << "  ";
	}
	cout << endl;
	return 0;
}

 我们发现遍历时并没有改变arr数组的值

我们只需将

	for (auto v : arr)
	{
		v *= 2;
	}

改为

	for (auto& v : arr)
	{
		v *= 2;
	}

即可 

而auto也不是一定的,只要我们知道要遍历的数组或容器对象的类型指定即可

如下代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int arr[] = { 1,2,3,74,6 };
	for (int& v : arr)
	{
		v *= 2;
	}
	for (int v : arr)
	{
		cout << v << "  ";
	}
	cout << endl;

	string s1 = "hello world";
	for (char s : s1)
	{
		cout << s << "  ";
	}
	cout << endl;
	return 0;
}

一. 标准库中string类的简单使用

在使用string类时,必须包含#include<string>头文件,由于string属于std标准库,所以通过域作用限定符可以使用string

#include<iostream>
#include<string>
int main()
{
	std::string ss1("56565");
	std::string ss2("12123");

	std::cout << ss1 << std::endl;
	std::cout << ss1 << std::endl;
	return 0;
}

也可以通过using 将其展开,然后使用

#include<iostream>
#include<string>
//using namespace std;
using std::cout;
using std::string;
using std::endl;
using std::cin;
int main()
{
	string ss1("56565");
	string ss2("12123");

	cout << ss1 << endl;
	cout << ss2 << endl;
	return 0;
}

二. string类的常用接口说明

1. string类对象的常见构造
函数名称功能说明
string()构造空的string类对象,即空字符串
string(const char* s)用s指向的字符序列(C字符串)构造string类对象
string(size_t n,char c)string类对象中包含n个字符c
string(const string& s)拷贝构造函数

如下代码所示

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

int main()
{
	//string s1();//不可以这样写
	string s2;//构造空的string类对象s2
	cout  << s2 << endl;
	string s3("hello world");//用C格式字符串构造string类对象s3
	cout << s3 << endl;
	string s4(11, 'x');//string类对象包含11个x字符
	cout << s4 << endl;
	string s5(s3);//拷贝构造s5
	cout << s5 << endl;
	return 0;
}
2. string类对象的容量操作
函数名称功能说明函数声明
size返回字符串有效字符长度
size_t size() const noexcept;
length返回字符串有效字符长度
size_t length() const noexcept;
capacity返回空间总大小
size_t capacity() const noexcept;
empty检测字符串是否为空串,是返回true,否则返回false
bool empty() const noexcept;
clear清空有效字符
void clear() noexcept;
reserve为字符串预留空间
void reserve (size_t n = 0);
resize将有效字符的个数改成n个,多出的空间用字符c填充
void resize (size_t n);
void resize (size_t n, char c);

size()与length() 方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致(比如非线性结构二叉树的length肯定不是全部节点的个数),一般情况下基本都是用size()

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

int main()
{
	string s2;
	cout << s2.size() << "  " << s2.length() << endl;
	string s3("hello world");
	cout << s3.size() << "  " << s3.length() << endl;
	string s4(13, 'x');
	cout << s4.size() << "  " << s4.length() << endl;
	return 0;
}

clear()只是将string中有效字符清空,不改变底层空间大小

 如以下代码

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

int main()
{
	string s4(13, 'x');
	cout << "字符串为:" << s4 << "\n容量为" << s4.capacity() << endl;
	s4.clear();
	if(s4.empty())
	cout << "字符串为:" << s4 << "\n容量为" << s4.capacity() << endl;
	return 0;
}

其结果为

 resize(size_t n)与resize(size_t  n,char  c)都是将字符串中有效字符个数改到n个,不同的是当字符个数增多时:resize(size_t  n) 用 0 来填充多出的元素空间,resize(size_t  n,char  c)用字符c来填充多出的元素空间。注意: resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变

 代码如下

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

int main()
{
	string s1(13, 'x');
	string s2 (s1);
	string s3 (s1);

	s2.resize(5);
	s3.resize(5, '$');
	cout << "字符串为:" << s2 << "\n容量为" << s2.capacity() << endl;
	cout << "字符串为:" << s3 << "\n容量为" << s3.capacity() << endl;

	s2.resize(10);
	s3.resize(10, '$');
	cout << "字符串为:" << s2 << "\n容量为" << s2.capacity() << endl;
	cout << "字符串为:" << s3 << "\n容量为" << s3.capacity() << endl;

	s2.resize(16);
	s3.resize(16, '@');
	cout << "字符串为:" << s2 << "\n容量为" << s2.capacity() << endl;
	cout << "字符串为:" << s3 << "\n容量为" << s3.capacity() << endl;

	return 0;
}

 reserve(size_t  res_arg=0)为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserve不会改变容量大小

 代码如下

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

int main()
{
	string s1("111");
	cout << "字符串为:" << s1 << "\n容量为" << s1.capacity() << endl;

	s1.reserve(18);
	cout << "字符串为:" << s1 << "\n容量为" << s1.capacity() << endl;

	s1.reserve(1);
	cout << "字符串为:" << s1 << "\n容量为" << s1.capacity() << endl;
	return 0;
}

 结果为

 3. string类对象的访问及遍历操作
函数名称功能说明函数声明
operator[]返回pos位置的字符,const string类对象调用
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
begin与end

begin获取一个字符的迭代器

end获取最后一个字符的下一个位置的迭代器

 iterator begin() noexcept;
const_iterator begin() const noexcept; 
iterator end() noexcept;
const_iterator end() const noexcept;
rbegin与rend

rbegin返回一个指向容器最后一个元素的反向迭代器

rend返回一个反向迭代器,指向容器中第一个元素之前的位置

reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
范围forC++11支持的更简洁的新遍历方式

代码如下

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

int main()
{
	string s1("135616549");
	for (int i = 0; i < s1.size(); i++)
		cout << s1[i];
	cout << endl;

	string::iterator i = s1.begin();
	for (; i != s1.end(); i++)
		cout << *i;
	cout << endl;

	string::reverse_iterator i1= s1.rbegin();
	for (; i1 != s1.rend();i1++)
		cout << *i1;
	cout << endl;

	return 0;
}

范围for此处就不做演示了,上文有

4. string类对象的修改与查找操作

函数名称

功能说明函数声明
push_back在字符串后尾插字符c
void push_back (char c);
append在字符串后追加一个字符串接口过多详见表格下图
operator+=在字符串后追加字符串str
insert在指定位置追加字符或字符串
assign使用指定字符串替换原字符串
replace使用字符串替换原字符串指定区间
erase删除字符串指定的部分
c_str返回C格式字符串
const char* c_str() const noexcept;
swap交换两个字符串
void swap (string& str);
find从字符串pos位置开始往后找字符c,找到返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,找到返回该字符在字符串中的位置
npos(不是函数)静态成员常数值,size_t类型的最大可能值,find与rfind没找到返回npos
static const size_t npos = -1;
substr在str中从pos位置开始,截取n个字符,然后将其返回
string substr (size_t pos = 0, size_t len = npos) const;
 插入字符或字符串

 

 

 

 接口过多在使用时可以查阅文档使用

在string尾部追加字符时s.push_back(c) 与  s.append(1, c) 与 s += 'c'三种的实现方式差 不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可 以连接字符串。

 如下代码所示

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

int main()
{
	string s1("135616549");
	string s2(s1);
	string s3(s1);
	string s4(s1);
	string s5(s1);

	s2.push_back('x');
	s3.append("x");
	s4 += 'x';
	s5.insert(9,"x");
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	s4 += "cccc";
	cout << s4 << endl;
	return 0;
}

对string操作时,若可以提前知道放多少字符,可以先通过reserve把空间预留好

字符串替换

 演示代码如下

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

int main()
{
	string s1("@123456@");
	string s2("555555555");
	string s3(s1);
	cout << s1 << endl;
	s1.assign(s2);
	cout << s1 << endl;
	s1.assign("666666665");
	cout << s1 << endl;

	s1.replace(1, 5, s3);
	cout << s1 << endl;

	return 0;
}

输出结果如下

字符串的交换

string::swap通常直接交换两个string对象的内部数据(如字符数组指针和长度信息),而不需要复制字符数据。这使得交换非常高效。在C++98中优于全局swap(而在C++11中,全局的swap检测到是两个string交换也会调用string的swap)

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

int main()
{
	string s1("@123456@");
	string s2("55555555");

	cout << "字符串为:  " << s1 << endl;
	cout << "字符串为:  " << s2 << endl;
	cout << endl;

	s1.swap(s2);
	cout << endl;
	cout << "字符串为:  " << s1 << endl;
	cout << "字符串为:  " << s2 << endl;
	cout << endl;

	swap(s1, s2);
	cout << endl;
	cout << "字符串为:  " << s1 << endl;
	cout << "字符串为:  " << s2 << endl;
	return 0;
}
查找的使用

可以与多种需要坐标定位的函数联合使用

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

int main()
{
	string s1("@123456@");
	size_t pos1=s1.find('@');
	size_t pos2 = s1.rfind('@');

	for (int i = pos1; i < s1.size(); i++)
		cout << s1[i];
	cout << endl;

	for (int i = pos2; i < s1.size(); i++)
		cout << s1[i];
	cout << endl;

	size_t pos3 = s1.find('@', 3);
	for (int i = pos3; i < s1.size(); i++)
		cout << s1[i];
	cout << endl;

	size_t pos4 = s1.find('&');
	size_t pos5 = s1.rfind('&');
	cout << pos4 << "  " << pos5 << "  " << string::npos << endl;

    //与substr的联合使用
	size_t pos = s1.find('4');
	cout << s1.substr(pos, 3) << endl;

    //与insert的联合使用
	s1.insert(pos, "xxxx");
	cout << s1 << endl;
	return 0;
}
5. string类非成员函数
函数功能

operator+

链接字符串返回一个新构造的字符串对象(尽量少用)

,因为传值返回导致深拷贝效率低

getline获取一行字符串
operator>>输入运算符重载
operator<<输出运算符重载
relational operators大小比较

 我们这里只介绍getline

代码:

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

int main()
{
	string s1;
	string s2;

	cin >> s1;
	cout << s1<<endl;

	getline(cin, s2);
	cout << s2<<endl;

	string s3;
	//遇到'&'才会结束
	getline(cin, s3, '&');
	cout << s3 << endl;
	
	return 0;
}

结果为

 getline与流插入都是读取字符串,但是流插入遇见空格就停止,并且不把空格读进去而getline会遇见空格不会停止,由上面我们的第一个输入

hello hello first

可得:流插入读取到第一个空格就结束了,第一个空格被getline读进去了(从输出也可以看出来)

普通getline遇见回车结束,我们可以指定遇见什么结束


这篇文章就到这里啦~喜欢的可以点一下赞

(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤

  • 56
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值