【STL】string容器

1. 前言—STL

1.1 STL基本概念

长久以来,软件界一直希望建立一种可重复利用的东西。C++的面向对象泛型编程思想,目的就是提高复用性。大多情况下,数据结构和算法都没能有一套标准,导致被迫从事大量重复工作。为了建立数据结构和算法的一套标准,诞生了STL。

  • STL(Standard Template Library,标准模板库)
  • STL从广义上分为:容器(container)、算法(algorithm)、迭代器(iterator)
  • 容器和算法之间通过迭代器进行无缝连接
  • STL几乎所以的代码都采用了模板类或者仿函数

1.2 STL的六大组件

  1. 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据
  2. 算法:各种常用的算法,如sort、find、copy、for_each等
  3. 迭代器:扮演了容器与算法之间的胶合剂
  4. 仿函数:行为类似函数,可作为算法的某种策略
  5. 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西
  6. 空间适配器:负责空间的配置与管理

2. string容器

2.1 string容器的基本概念

本质:

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

string与char*的区别:

  1. char* 是一个指针
  2. string是一个类,类内封装了char*,管理这个字符串,是一个char*型的容器

特点:

string类内封装了很多成员方法,例如:查找find,拷贝copy,删除delete,替换replace,插入insert

string管理char*所分配的内存,不用担心复制越界和取值越界等的问题,由类内进行负责

2.2 string容器构造函数

构造函数原型为:

string(); //创建一个空的字符串 例如: string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化

示例:

/*
string(); //创建一个空的字符串 例如: string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化
*/

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

void test01()
{
	string s1;//创建一个空的字符串s1,调用无参构造函数
	
	const char* str = "hello world";
	string s2(str);//使用字符串str初始化s2
	cout << "s2 = " << s2 << endl;

	string s3(s2);//调用拷贝构造函数
	cout << "s3 = " << s3 << endl;

	string s4(10, 'a');//使用10个字符a初始化
	cout << "s4 = " << s4 << endl;
}

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

2.3 string容器赋值操作

函数原型:

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

示例:

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

void test01()
{
	string s1;//创建一个空的字符串s1,调用无参构造函数

	const char* str = "hello world";
	string s2(str);//使用字符串str初始化s2
	cout << "s2 = " << s2 << endl;

	string s3(s2);//调用拷贝构造函数
	cout << "s3 = " << s3 << endl;

	string s4(10, 'a');//使用10个字符a初始化
	cout << "s4 = " << s4 << endl;

	string s5;
	s5.assign("hello c++", 5);
	cout << "s5 = " << s5 << endl;

	string s6;
	s6.assign(s5);
	cout << "s6 = " << s6 << endl;

	string s7;
	s7.assign(5, 'x');
	cout << "s7 = " << s7 << endl;
}

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

总结:string的赋值方式有很多,operator=这种方式是比较使用的

2.4 string字符串拼接

功能描述

  • 实现在字符串末尾拼接字符串

函数模型:

string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
string& operator+=(const string& str); //重载+=操作符
string& append(const char *s); //把字符串s连接到当前字符串结尾
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=(const string& str)
string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

示例:

//string& operator+=(const char* str); //重载+=操作符
//string& operator+=(const char c); //重载+=操作符
//string& operator+=(const string& str); //重载+=操作符
//string& append(const char* s); //把字符串s连接到当前字符串结尾
//string& append(const char* s, int n); //把字符串s的前n个字符连接到当前字符串结尾
//string& append(const string& s); //同operator+=(const string& str)
//string& append(const string& s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

//字符串拼接
void test01()
{
	string s1 = "我";
	s1 += "爱玩游戏";
	cout << "s1 = " << s1 << endl;

	s1 += ":";
	cout << "s1 = " << s1 << endl;

	string s2 = "LOL DNF";
	s1 += s2;
	cout << "s1 = " << s1 << endl;

	string s3 = "I";
	s3.append(" love ");
	s3.append("game abcde", 4);
	cout << "s3 = " << s3 << endl;

	s3.append(s2, 4, 3);//从下标4开始,截取3个字符,拼接到字符串末尾
	cout << "s3 = " << s3 << endl;
}


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

2.5 string容器的查找与替换

功能描述

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

函数原型:

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

示例:

//string查找与替换
void test01()
{
	//查找
	//1.查找第一次出现的位置
	string s1 = "abcdefgde";
	int pos1 = s1.find("de");
	if (pos1 == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "pos1 = " << pos1 << endl;
	}
	//2.查找最后一次出现的位置
	int pos2 = s1.rfind("de");
	if (pos2 == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "pos2 = " << pos2 << endl;
	}
}
void test02()
{
	//替换
	string s1 = "abcdefde";
	s1.replace(1, 3, "1111");
	cout << "s1 = " << s1 << endl;
}

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

总结:

  • find查找是从左往右,rfind是从右往左
  • find找到字符串后返回的是查找的第一个字符的位置,若没有找到返回-1
  • replace在替换时,要指定从哪个位置,多少个字节,替换成什么样的字符串

2.6 string字符串的比较

功能描述

比较方式:

字符串的比较是按照字符ASCII码进行对比的

相等 返回0

大于 返回1

小于 返回-1

函数模型:

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

示例:

//字符串比较
void test01()
{
	string s1 = "hello";
	string s2 = "string";
	int ret = s1.compare(s2);
	if (ret == 0)
	{
		cout << "s1 = s2" << endl;
	}
	else if (ret > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}

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

总结:字符串对比主要用于两个字符串是否相等,判断谁大谁小没有意义

2.7 string字符存取

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

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

示例:

//字符存取
void test01()
{
	string str = "hello world";
	//字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
}

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

总结:string字符串存取中单个字符存取有两种方式,利用[]或at

2.8 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个字符

示例:

//字符串插入和删除
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);//从下标1位置开始删除三个字符
	cout << str << endl;
}

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

注:插入和删除的起始下标都是从0开始的

2.9 string子串

功能描述

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

函数模型:

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

示例:

//获取子串
void test01()
{
	string s1 = "abcdefg";
	string subStr = s1.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	//应用
	string email = "zhangsan@qq.com";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username = " << username << endl;
}

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

总结:灵活的运用求子串功能,可以在实际开发中获取有效的信息

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...