【C++初阶】--- string类

1.STL简介

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架
在这里插入图片描述

2.string类

2.1什么是string类

std::string 类是 C++ 标准库中用于处理和操作字符串的一个类,它定义在 头文件中。相较于 C 语言里以空字符 ‘\0’ 结尾的字符数组(C 风格字符串),std::string 类提供了更便捷、安全且强大的字符串处理功能。

2.2string类和STL的关系

std::string 是 C++ 标准库中的一个类,它位于 头文件中,虽然它本身并不严格属于 STL 的核心组件(容器、算法、迭代器等),但它与 STL 紧密相关,具备很多与 STL 容器相似的特性

3.auto和范围for

3.1auto关键字

  1. auto声明的变量必须由编译器在编译时期推导而得。
  2. 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
  3. 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
  4. auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
  5. auto不能直接用来声明数组
#include<iostream>
#include<string>
using namespace std;

int func1()
{
	return 10;
}

// 不能做参数
//void func2(auto a)
//{}

// 可以做返回值,但是建议谨慎使用
auto func3()
{
	return 3;
}

int main()
{
	int a = 10;
	auto b = a;
	auto c = 'a';
	auto d = func1();
	// 编译报错:rror C3531: “e”: 类型包含“auto”的符号必须具有初始值设定项
	//auto e;
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
	cout << typeid(d).name() << endl;
	int x = 10;
	auto y = &x;
	auto* z = &x;
	auto& m = x;
	//可以打印类型
	cout << typeid(x).name() << endl;
	cout << typeid(y).name() << endl;
	cout << typeid(z).name() << endl;
	cout << typeid(m).name() << endl;
	auto aa = 1, bb = 2;
	// 编译报错:error C3538: 在声明符列表中,“auto”必须始终推导为同一类型
	//auto cc = 3, dd = 4.0;
	// 编译报错:error C3318: “auto []”: 数组不能具有其中包含“auto”的元素类型
	//auto array[] = { 4, 5, 6 };

	return 0;
}

3.2范围for

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

int main() 
{
	//数组的遍历
	int arr[] = { 1,2,3,4,5,6,7 };
	for (auto ch : arr)//不可修改arr的内容
	{
		cout << ch << " ";
	}
	cout << endl;

	//可以修改arr的内容
	for (auto& ch : arr)
	{
		ch *= 2;
		cout << ch << " ";
	}
	cout << endl;

	//对象的遍历
	string st("hello world");
	for (auto ch : st)
	{
		cout << ch << " ";
	}
	cout << endl;

	return 0;
}

4.遍历数组的三种方式

  1. 下标+[]
  2. 迭代器遍历
  3. 范围for

注意:范围for的ch只是st的一份拷贝,改变ch不会影响st。而迭代器会影响st

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

int main()
{
	string st("hello world");

	//1.下标+[]
	for (size_t i = 0; i < st.size(); i++)
	{
		cout << st[i] << " ";
	}cout << endl;

	//2.迭代器
	string::iterator it = st.begin();
	while (it != st.end())
	{
		*it += 2;//可以修改内容
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//3.范围for
	for (auto ch : st)
	{
		ch -= 2;//可以修改,但不影响原内容
		cout << ch << " ";
	}
	cout << endl;

	return 0;
}

5.string类对象的常见构造

在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;

int main()
{
	//(1).无参构造
	string s1;
	
	//(2).拷贝构造
	string s3 = s1;
	
	//(3).从str的pos位置向后拷贝len个字符
	string s4("hello world", 6, 15);//从s2的第6个位置向后复制n个
	//string::npos, size_t npos = -1,即max_int
	
	//(4).带参构造
	string s2("hello world");

	//(5).拷贝前n个字符
	string s5("hello world", 7);//拷贝前n个

	//(6).拷贝n个字符's'
	string s6(7, 'x');//拷贝n个字符

	return 0;
}

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

在这里插入图片描述
在这里插入图片描述

迭代器分为四种:正向迭代器、正向只读迭代器、反向迭代器、反向只读迭代器
在这里插入图片描述

6.1begin()/end()和rbegin()/rend()

  1. begin()的用途是返回指向容器首元素的迭代器。
  2. end()的用途是返回指向容器末尾元素的下一个位置的迭代器
  3. rbegin()的用途是返回指向容器末尾元素的迭代器
  4. rend()的用途是返回指向容器首元素的前一个位置的迭代器。在这里插入图片描述
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string st("hello world");

	//下标+[]遍历
	for (size_t i = 0; i < st.size(); i++)
	{
		cout << st[i] << " ";
	}cout << endl;

	//正向遍历
	string::iterator it = st.begin();
	while (it != st.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//反向遍历
	string::reverse_iterator rit = st.rbegin();
	while (rit != st.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	return 0;
}

在这里插入图片描述

7.string类对象的容量操作

在这里插入图片描述
在这里插入图片描述

  1. size()和length()返回字符串长度
  2. max_size()返回可存储的字符串的最大长度
  3. resize()用于调整字符串的大小,即保留多少容量(会删除字符)
    在这里插入图片描述
  4. capacity()返回已分配存储的大小,不包括’\0’
  5. reserve()用于更改容量capacity
    在这里插入图片描述
  6. clear()用于清除所有数据,一般不清容量
  7. empty()用于测试string是否为空
  8. shrink_to_fit()用于将容量收缩至长度大小
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string st("hello world");

	//size和length返回字符串的有效字符长度
	cout << "st字符长度->" << st.size() << endl;
	cout << "st字符长度->" << st.length() << endl << endl;

	//capacity()返回总空间大小
	cout << "st空间大小->" << st.capacity() << endl << endl;

	//empty()判空,空返回true,否则返回false
	if (st.empty() == true)
	{
		cout << "st字符串为空" << endl << endl;
	}
	else
	{
		cout << "st字符串不为空" << endl << endl;
	}

	//clear()清空有效字符,一般不会清空间
	st.clear();
	cout << "st字符长度->" << st.size() << endl;
	cout << "st空间大小->" << st.capacity() << endl << endl;

	string st1("xxxxxxxxxxxxxxxxxxx");
	cout << "str1字符长度->" << st1.size() << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	//reserve(n) 只有n>capacity时才一定改变,空间会变成>=n
	//其他情况看编译器自行优化
	st1.reserve(10);
	cout << "str1字符长度->" << st1.size() << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	st1.reserve(20);
	cout << "str1字符长度->" << st1.size() << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;
	
	st1.reserve(90);
	cout << "str1字符长度->" << st1.size() << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	st1.resize(30);
	cout << "str1字符长度->" << st1.size() << endl;
	cout << st1 << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	st1.resize(35, 'y');
	cout << "str1字符长度->" << st1.size() << endl;
	cout << st1 << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	st1.resize(10);
	cout << "str1字符长度->" << st1.size() << endl;
	cout << st1 << endl;
	cout << "str1空间大小->" << st1.capacity() << endl << endl;

	return 0;
}

在这里插入图片描述

8.string类对象的修改操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

int main()
{
	string st("hello world");
	string st1("hello everyone");

	//operator+=,在尾部追加对象、字符串、字符
	st += st1;
	cout << st << endl;

	st += "XXXXXXXXXXX";
	cout << st << endl;

	st += 'y';
	cout << st << endl;

	string st2("welcome ");
	string st3("to here");

	//append,在尾部追加字符串
	st2.append(st3);
	cout << st2 << endl;

	//push_back将字符 c 追加到字符串的末尾,将其长度增加 1
	st2.push_back('H');
	cout << st2 << endl;

	//assign为字符串分配一个新值,替换其当前内容。
	st2.assign(st3);
	cout << st2 << endl;

	//insert头插
	st2.insert(0, st3);
	cout << st2 << endl;

	//erase擦除字符串的一部分,减少其长度:
	st2.erase(0, st3.size());
	cout << st2 << endl;

	//replace将字符串中从字符pos开始并跨越len字符的部分替换为新内容:
	st2.replace(0, 1, "%");
	cout << st2 << endl;

	//swap
	st2.swap(st3);
	cout << st2 << endl;
	cout << st3 << endl;
	swap(st2, st3);
	cout << st2 << endl;
	cout << st3 << endl;

	//pop_back擦除字符串的最后一个字符,从而有效地将其长度减少 1。
	st2.pop_back();
	cout << st2 << endl;

	return 0;
}
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string st("hello world");

	//c_str()获取等效字符串
	cout << st.c_str() << endl;

	//data获取字符串数据
	cout << st.data() << endl;

	//copy将字符串对象当前值的子字符串复制到 s 指向的数组中。此子字符串包含从位置 pos 开始的 len 字符。
	char buff[20];
	//copy(s,len,pos)
	st.copy(buff, 5, 0);
	buff[6] = '\0';
	cout << buff << endl;

	//find返回找到的位置下标
	int ret = st.find(' ');
	cout << ret << endl;

	//没找到返回-1
	string st1("worlddx");
	ret = st.find(st1, 0);
	cout << ret << endl;

	//rfind,反着找
	ret = st.rfind('d');
	cout << ret << endl;

	//find_first_of在字符串中搜索与其参数中指定的任何字符匹配的第一个字符。
	ret = st.find_first_of("whcasuy");
	cout << ret << endl;

	//find_last_of在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。
	ret = st.find_last_of("whcasuy");
	cout << ret << endl;

	//find_first_not_of在字符串中搜索与参数中指定的任何字符都不匹配的第一个字符。
	ret = st.find_first_not_of("hello ");
	cout << ret << endl;

	//find_last_not_of在字符串中搜索与参数中指定的任何字符都不匹配的最后一个字符。
	ret = st.find_last_not_of("heo word");
	cout << ret << endl;

	//substr返回一个新构造的对象,其值初始化为此对象的子字符串的副本。
	cout << st.substr(0) << endl;
	cout << st.substr(0, 5) << endl;

	//compare将字符串对象(或子字符串)的值与其参数指定的字符序列进行比较。
	ret = st.compare(0, 5,"hello");
	cout << ret << endl;
	
	return 0;
}

9.string类非成员函数

在这里插入图片描述

在这里插入图片描述

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

int main()
{
	//operator+连接两对象,或者对象和字符串
	string st("hello");
	string st1(" world");
	cout << st + " world" << endl;
	cout << st + st1 << endl;

	swap(st, st1);
	cout << st << endl;
	cout << st1 << endl;

	//operator>>从流中提取字符串
	//opsrator<<将字符串插入流中
	string st2;
	cin >> st2;
	cout << st2 << endl;

	//getline从istream中提取字符并将其存储到str中,直到找到分隔符
	string st3;
	//遇到换行停下
	getline(cin, st3, '\n');
	cout << "st3:" << st3 << endl;

	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值