【C++】黑马C++泛型编程和STL技术 (3) STL常用容器 --- string容器

3.STL常用容器

3.1 string 容器

3.1.1 string基本概念

本质:
C风格的字符串是char*(指针)
string是C++风格的字符串,本质上是一个,这个类的内部封装了char*,管理这个字符串,是一个char*型的容器。

特点:
string类内部封装了很多成员函数来管理字符串
例如,查找find、拷贝copy、删除delete、替换replace、插入insert
string 管理char*所分配的内存,不用担心复制越界和取值越界,由类内部进行负责

3.1.2 string构造函数

有以下四种方式:

#include<iostream>
using namespace std;
#include<string>
// string 的构造函数
void test01()
{
	// 默认构造 --- 创建出一个空字符串	
	string s1;								// string();
	// 使用字符串char*初始化
	const char* str = "hello world";
	string s2(str);							// string(const char* s)
	cout << "s2: "<< s2 << endl;
	// 拷贝构造===用一个string对象初始化另一个string对象
	string s3(s2);							// string(const string&str)
	cout << "s3: "<< s3 << endl;
	// 使用n个字符c初始化
	string s4(10,'a');						// string(int n,char c)
	cout << "s4: "<< s4 << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.1.3 string赋值操作

重载= 和 成员函数assign 两种方式

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

// string 赋值操作

//string& operator=(const char* s);
//string& operator=(const strign & s);
//string& operator=(char c);
//string& assign(const char* s);
//string& assign(const char* s,int n);	// 把字符串s的前n个字符赋值给当前的字符串
//string& assign(const string& s);
//string& assign(int n, char c);

void test01()
{
	string str1;
	str1 = "hello world!";
	cout << "str1: " << str1 << endl;
	
	string str2;
	str2 = str1;
	cout << "str2: " << str2 << endl;
	
	string str3;
	str3 = 'a';
	cout << "str3: " << str3 << endl;
	
	string str4;
	str4.assign("hello C++");
	cout << "str4: " << str4 << endl;
	
	string str5;
	str5.assign("hello C++",5);
	cout << "str5: " << str5 << endl;
	
	string str6;
	str6.assign(str5);
	cout << "str6: " << str6 << endl;
	
	string str7;
	str7.assign(10,'h');
	cout << "str7: " << str7 << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.1.4 string字符串拼接

在一个字符串的末尾拼接(追加)字符串
重载 += 和 成员函数 append 两种方式

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

//string& operator+=(const char* str);
//string& operator+=(const strign & s);
//string& operator+=(char c);
//string& append(const char* s);
//string& append(const char* s,int n);	// 把字符串s的前n个字符赋值给拼接的字符串
//string& append(const string& s);
//string& append(const string &s, int pos,int n); 
										// 字符串s中从pos开始的n个字符连接到字符串结尾
										// 字符串的第一个字符标号为0
void test01()
{
	string str = "hello";

	str += " wrold";
	cout << "str: "<< str << endl;

	str += ',';
	cout << "str: "<< str << endl;
	
	string str1 = "welcome";
	str += str1;
	cout << "str: "<< str << endl;
	
	str.append(" to");
	cout << "str: "<< str << endl;
	
	str.append(" Cpp aaa",4);
	cout << "str: "<< str << endl;
	
	string str2 = "! This ";
	str.append(str2);
	cout << "str: "<< str << endl;
	
	str.append(str2,0,3);
	cout << "str: "<< str << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

3.1.5 string查找和替换

查找:查找指定字符串是否存在
替换:在指定位置替换字符串

int find(const string& str,int pos = 0) const;	// 查找str第一次出现位置,从pos开始查找
int find(const char*s,int pos,int n) const;		// 从pos位置查找s的前n个字符第一次出现的位置 
int rfind(const string& str,int pos = npos) const;	// 查找str最后一次位置,从pos开始查找

findrfind的区别:
find是从左往右查找,rfind是从右往左查找。从右往左查找的第一个其实就是从左往右的最后一个。但是两者的返回值都是按照从0开始的从左往右的顺序编号

repalce的函数原型如下:

string& replace(int pos,int n,const string& str);
// 替换从pos开始的n个字符为字符串str
string& replace(int pos,int n,const char* s);
// 替换从pos开始的n个字符为字符串s

代码示例:

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

// 1. 查找
void test01()
{
	string str1 = "ABCDEFGHIKLMNDE";
	cout << "str1: " << str1 << endl;
// int find(const string& str,int pos = 0) const; 
// 查找str第一次出现位置,从pos开始查找
// const 表示该函数不可修改变量
// 因为有了默认参数,所以可以不用传值
	int pos1 = str1.find("DE");
	cout << "pos1 = " << pos1 << endl;
	// 找到返回下标,这里返回3,注意是从0开始编号
	int pos2 = str1.find("DF");
	cout << "pos2 = " << pos2 << endl;	
	// 找不到,返回-1
	
	// rfind
	int pos3 = str1.rfind("DE");
	cout << "pos3 = " << pos3 << endl;
}

// 2. 替换
void test02()
{
	str1.replace(1,3,"1234");
	// 从1号位置起,3个字符替换为字符串 1234
	cout << "str1: " << str1 << endl;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

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

3.1.6 string字符串比较

按照字符的ASCII码进行对比。 = 返回0,> 返回1,< 返回-1。主要是用于判断字符串是否相等,判断大小的意义并不大。
函数原型:

int compare(const string &s) const;		// 与字符串s比较
int compare(const char *s) const;		// 与字符串s比较

示例:

#include<iostream>
using namespace std;

void test01()
{
	string str1 = "hello";
//	string str1 = "aello";
//	string str1 = "xello";
	string str2 = "hello";
	if(str1.compare(str2) == 0)
		cout << "str1等于str2"<< endl;
	else if(str1.compare(str2)>0)
		cout << "str1大于str2"<< endl;
	else if(str1.compare(str2)<0)
		cout << "str1小于str2"<< endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.1.7 string字符存取

对单个字符进行读写操作,有以下两种方式:

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

示例:

#include<iostream>
using namespace std;
#include<string>
// 字符存取
void test01()
{
	string str = "hello";
	cout << "str = " << str << endl;
	// 通过[]读每个字符
	for(int i = 0;i<str.size();i++)		// 获取字符串长度的成员函数
	{
		cout << str[i] << " ";
	}
	cout << endl;
	// 通过at读取单个字符
	for(int i = 0;i<str.size();i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;
	// 通过[] 修改字符
	str[0]  = 'x';
	cout << "str = " << str << endl;
	// 通过at修改字符
	str.at(1)  = 'y';
	cout << "str = " << str << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

3.1.8 string的插入和删除

对string字符串进行插入和删除字符操作
函数原型:

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 = npos);	// 删除从pos开始的n个字符

示例:

#include<iostream>
using namespace std;
void test01()
{
	string str = "ABCD";
	cout << "str = " << str << endl;
	
	cout << "在第2个位置插入EFG:" <<endl;
	str.insert(2,"EFG");
	cout << "str = " << str << endl;
	
	cout << "删除第2个位置开始的三个字符:" <<endl;
	str.erase(2,3);
	cout << "str = " << str << endl;
	
	cout << "在第4个位置插入3个A:" <<endl;
	str.insert(4,3,'A');
	cout << "str = " << str << endl;
	
	cout << "在第4个位置插入3个B:" <<endl;
	string str1 = "BBB";
	str.insert(4,str1);
	cout << "str = " << str << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

3.1.9 string子串

从字符串中获取想要的子串
函数原型:

string substr(int pos = 0,int n = npos) const; //返回由pos开始的n个字符组成的数组

示例:

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string str = "abcdef";
	string subStr = str.substr(1,3);
	cout << "subStr = " << subStr << endl;
}
// 实用操作
void test02()
{
	string email = "jinyawei@xidian.student.en";
	// 从邮箱地址中获取用户名信息
	int pos = email.find("@");
	string userName = email.substr(0,pos);
	cout << "userName = " << userName << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值