第一百一十六天学习记录:C++提高:STL-string(黑马教学视频)

news2024/10/10 20:19:59

string基本概念

string是C++风格的字符串,而string本质上是一个类
string和char区别
1、char
是一个指针
2、string是一个类,类内部封装了char*,管理这个字符串,是一个char型的容器。
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete替换replace,插入insert
string管理char
所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。

string构造函数

构造函数原型:

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<vector>
#include<string>

//string的构造函数


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


void test01()
{
	string s1;//默认构造
	const char* str = "hello world";
	string s2(str);
	cout << "s2=" << s2 << endl;
	string s3(s2);
	cout << "s3=" << s3 << endl;
	string s4(20, 'a');
	cout << "s4=" << s4 << endl;
}

int main()
{
	test01();
	return 0;
}

总结:string的多种构造方式没有可比性,灵活使用即可。

string赋值操作

功能描述:
给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 n);		//用n个字符c赋给当前字符串
#include<iostream>
using namespace std;
#include<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 n);		//用n个字符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(18,'z');
	cout << "str6=" << str7 << endl;
}

int main()
{
	test01();
	return 0;
}

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

string字符串拼接

功能描述:
实现在字符串末尾拼接字符串

函数原型:

string& operator+=(const char* str);	//重载+=操作符
string& operator+=(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);			//同string& operator+=(const string& str)
string& append(const string& s,int pos,int n);				//字符串s中从pos开始的n个字符连接到字符串结尾
#include<iostream>
using namespace std;
#include<string>

//string& operator+=(const char* str);	//重载+=操作符
//string& operator+=(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);			//同string& operator+=(const string& str)
//string& append(const string& s, int pos, int n);				//字符串s中从pos开始的n个字符连接到字符串结尾

void test01()
{
	string str1 = "我";
	str1 += "是谁";
	cout << "str1=" << str1 << endl;
	str1 += '?';
	cout << "str1=" << str1 << endl;
	string str2 = "Who am I?";
	str1 += str2;
	cout << "str1=" << str1 << endl;

	string str3 = "你";
	str3.append("是");
	cout << "str3=" << str3 << endl;
	str3.append("谁的谁?????",4);
	cout << "str3=" << str3 << endl;
	str3.append(str2);
	cout << "str3=" << str3 << endl;
	str3.append(str2,4,2);//截取
	cout << "str3=" << str3 << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

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
#include<iostream>
using namespace std;
#include<string>

//字符串查找和替换

//1、查找

void test01()
{
	string str1 = "abcdefgde";
	//int pos = str1.find("de");
	int pos = str1.find("de",0);//3 没有则返回-1
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串,pos = " << pos << endl;
	}

	pos = str1.rfind("de");//7
	cout << "pos = " << pos << endl;
}
//rfind 和 find 区别
//rfind从右往左查找  find从左往右查找

//2、替换

void test02()
{
	string str1 = "abcdefg";
	//从1号位置起3个字符 替换为"1111111"
	str1.replace(1, 3, "1111111");
	cout << "str1 = " << str1 << endl;
}

int main()
{
	//test01();
	test02();
	return 0;
}

string字符串比较

功能描述:
字符串之间的比较
比较方式:
字符串比较是按字符的ASCII码进行对比

= 返回 0
> 返回 1
< 返回 -1

函数原型:
1、int compare(const string& s)const; //与字符串s比较
2、int compare(const char* s)const; //与字符串s比较

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

//字符串比较

void test01()
{
	string str1 = "hello";
	string str2 = "hello";

	if (str1.compare(str2) == 0)
	{
		cout << "str1 等于 str2 " << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1 大于 str2 " << endl;
	}
	else
	{
		cout << "str1 小于 str2 " << endl;
	}
}

int main()
{
	test01();
	return 0;
}

注:主要是用来比较是否相等,大于小于的意义不大。

string字符存取

string 中单个字符存取方式有两种
1、char& operator[](int n); //通过[]方式取字符
2、char& at(int n);通过at方式获取字符

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

//string 字符存取

void test01()
{
	string str = "hello";
	//cout << "str=" << str << endl;

	//1、通过[]访问单个字符

	for (int i = 0; i < str.size(); ++i)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	//2、通过at方式访问单个字符

	for (int i = 0; i < str.size(); ++i)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	//修改单个字符
	str[0] = 'x';

	cout << "str=" << str << endl;

	str.at(1) = 'x';

	cout << "str=" << str << endl;
}

int main()
{
	test01();
	return 0;
}

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;
#include<string>

//字符串 插入和删除

void test01()
{
	string str = "hello";

	//插入
	str.insert(1, "111");

	cout << "str=" << str << endl;//h111ello

	//删除
	str.erase(1, 3);

	cout << "str=" << str << endl;
}

int main()
{
	test01();
	return 0;
}

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

string子串

功能描述:
从字符串中获取想要的子串
函数原型:
string substr(int pos=0,int n =npos)const;//返回由pos开始的n个字符组成的字符串

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

//string 求子串

void test01()
{
	string str = "abcdef";

	string subStr = str.substr(1, 3);

	cout << "subStr=" << subStr << endl;
}

//实用操作
void test02()
{
	string email = "hello@sina.com";

	//从邮件地址中 获取 用户信息

	int pos = email.find('@');

	cout << pos << endl;

	string usrName = email.substr(0, pos);

	cout << usrName << endl;

}

int main()
{
	//test01();
	test02();
	return 0;
}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/790170.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

idea社区版(2023.1)设置spring boot项目热启动

热启动 在开发过程中&#xff0c;当写完一个功能我们需要运行应用程序测试时需要重启服务器&#xff0c;一个最简单的项目也要花费10多秒&#xff0c;如果是更大的项目则耗时更多。SpringBoot提供了spring-boot-devtools&#xff0c;使得项目在发生改动时能够自动重启应用 id…

hadoop3编译安装

1.参考资料 官方的https://github.com/apache/hadoop/blob/trunk/BUILDING.txt 2.编译环境 Linux系统&#xff1a;Centos7.2 Jdk版本&#xff1a;jdk1.8 cmake版本&#xff1a;3.19 Hadoop版本&#xff1a;3.1.2 Maven版本&#xff1a;3.6.3 Protobuf版本&#xff1a;2…

【数据分析专栏之Python篇】二、Jupyer Notebook安装配置及基本使用

文章目录 前言一、Jupter Notebook是什么1.1 简介1.2 组成部分1.3 Jupyter Notebook的主要特点 二、为什么使用Jupyter Notebook?三、安装四、Jupyter Notebok配置4.1 基本配置4.2 配置开机自启与后台运行4.3 开启代码自动补全 五、两种键盘输入模式5.1 编辑模式5.2 命令模式5…

J2EE通用分页02

目录 一.重构-提取公用方法 1.为了进行公共方法的抽取&#xff0c;需要找出上面实习中的可通用部分&#xff0c;和差异化部分 2.公用方法封装思路 3. 具体实现 二.分页标签 2.1 准备一个Servlet 3.2 结果展示页面 三. 过滤器解决中文乱码问题 四.加入分页功能 四…

Qt Core学习日记——第七天QMetaObject(上)

每一个声明Q_OBJECT的类都具有QMetaObject对象 Q_OBJECT宏源代码&#xff1a; #define Q_OBJECT \ public: \ QT_WARNING_PUSH \ Q_OBJECT_NO_OVERRIDE_WARNING \ static const QMetaObject staticMetaObject; \ virtual const QMetaObject *metaObject() const; \ vir…

Rust vs Go:常用语法对比(五)

题图来自 Rust vs Go 2023[1] 81. Round floating point number to integer Declare integer y and initialize it with the rounded value of floating point number x . Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity). 按规…

垃圾回收标记阶段算法

1.标记阶段的目的 主要是在GC在前&#xff0c;判断出哪些是有用的对象&#xff0c;哪些是需要回收的对象&#xff0c;只有被标记为垃圾对象&#xff0c;GC才会对其进行垃圾回收。判断对象是否为垃圾对象的两种方式&#xff1a;引用计数算法和可达性分析算法。 2.引用计数算法…

MySQL基础(一)

目录 前言 一、概述 1.什么是数据库 2.数据库能干什么 3.为什么要用数据库&#xff0c;优势、特性 二、数据库类型 &#xff08;一&#xff09;、关系型数据库&#xff0c;RDBMS 1.概述 2.特点 3.代表产品 &#xff08;二&#xff09;、非关系型数据库&#xff0c;No…

性能测试常见故障和解决思路

目录 一、性能问题分析流程 二、内存溢出 &#xff08;一&#xff09;堆内存溢出 &#xff08;二&#xff09;永久代/方法区溢出 &#xff08;三&#xff09;栈内存溢出 &#xff08;四&#xff09;系统内存溢出 三、CPU过高 &#xff08;一&#xff09;us cpu过高 &a…

flink cdc环境搭建

1.下载flink https://archive.apache.org/dist/flink/flink-1.12.2/ 2.修改flink-conf.yaml #根据自己电脑核数修改&#xff0c;这里我设置为4&#xff0c;因为系统分配了4核 jobmanager.rpc.address: localhost #主机名根据自己设定 taskmanager.numberOfTaskSlots: 4 3.下载…

Databend 开源周报第 103 期

Databend 是一款现代云数仓。专为弹性和高效设计&#xff0c;为您的大规模分析需求保驾护航。自由且开源。即刻体验云服务&#xff1a;https://app.databend.cn 。 Whats On In Databend 探索 Databend 本周新进展&#xff0c;遇到更贴近你心意的 Databend 。 创建网络策略 …

机器学习深度学习——图像分类数据集

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位即将上大四&#xff0c;正专攻机器学习的保研er &#x1f30c;上期文章&#xff1a;机器学习&&深度学习——softmax回归&#xff08;下&#xff09; &#x1f4da;订阅专栏&#xff1a;机器学习&&深度学习…

【腾讯云 Cloud Studio 实战训练营】沉浸式体验编写一个博客系统

文章目录 前言新建工作空间登录(注册)Cloud Studio 账号&#xff1a;进入 Cloud Studio 控制台&#xff1a;配置工作空间参数&#xff1a;确认并创建工作空间&#xff1a;项目搭建 配置nuxt 脚手架运行项目报错信息解决错误脚手架运行预览问题 开启博客代码配置layout首页配置 …

关于java中的内存回收机制

C中如果创建了一个对象&#xff0c;当不再使用的时候&#xff0c;需要手动调用delete方法来进行销毁。而Java提供了一套完整的垃圾回收机制&#xff08;gc&#xff09;。它会自动扫描内存中我们所创建过且不再使用的对象&#xff0c;进行自动销毁。但是gc并不完美&#xff0c;现…

7plus透明屏有哪些全新的使用体验?

7plus透明屏是指苹果公司推出的iPhone 7 Plus手机配备了透明屏幕的一种特殊版本。透明屏幕是一种新型的显示技术&#xff0c;可以使屏幕看起来透明&#xff0c;让用户感觉手机屏幕上的内容仿佛悬浮在空中一样。 透明屏幕的出现给用户带来了全新的使用体验。 首先&#xff0c;透…

对象存活判断

对象存活判断 在堆里存放着几乎所有的 Java 对象实例&#xff0c;在 GC 执行垃圾回收之前&#xff0c;首先需要区分出内存中哪些是存活对象&#xff0c;哪些是已经死亡的对象。只有被标记为己经死亡的对象&#xff0c;GC 才会在执行垃圾回收时&#xff0c;释放掉其所占用的内存…

微信小程序客服系统-对接消息推送-对接模板订阅消息-嵌入webview客服链接

想要给自己的小程序增加客服系统功能 小程序客服对接导自己的系统等需求&#xff0c;可以参照我开发的客服系统&#xff0c;实现私有化部署搭建对接的微信小程序 小程序消息推送对接 首先登录小程序后台在小程序后台>开发管理>开发设置>服务器域名部分&#xff0c;配置…

基于Java+SpringBoot+vue前后端分离学科竞赛管理系统设计实现

博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专…

Linux中的库

一、库是什么 库是一种可执行代码的二进制形式&#xff0c;可以被操作系统载入内存执行。就是将源代码转化为二进制格式的源代码&#xff0c;相当于进行了加密&#xff0c;别人可以使用库&#xff0c;但是看不到库中的内容。 二、库的分类和优缺点 静态库&#xff1a;是在程…

Shell 排序法 - 改良的插入排序

说明 插入排序法由未排序的后半部前端取出一个值&#xff0c;插入已排序前半部的适当位置&#xff0c;概念简单但速度不快。 排序要加快的基本原则之一&#xff0c;是让后一次的排序进行时&#xff0c;尽量利用前一次排序后的结果&#xff0c;以加快排序的速度&#xff0c;Shel…