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

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.mfbz.cn/a/53300.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C++——STL容器之list链表的讲解

目录 一.list的介绍 二.list类成员函数的讲解 2.2迭代器 三.添加删除数据&#xff1a; 3.1添加&#xff1a; 3.2删除数据 四.排序及去重函数&#xff1a; 错误案例如下&#xff1a; 方法如下&#xff1a; 一.list的介绍 list列表是序列容器&#xff0c;允许在序列内的任何…

Git学习

一、Git工作流程 二、Git学习 1.获取本地仓库 要使用Git对我们的代码进行版本控制&#xff0c;首先需要获得本地仓库 在电脑的任意位置创建一个空目录(例如test)作为我们的本地Git仓库进入这个目录中&#xff0c;点击右键打开Git bash窗口执行命令git init如果创建成功后可在…

点云处理——terrasolid教程

加载terrasolid软件模块 3、通过microstation的utilities->mdl applications加载terrasolid四个模块,加载成功后将显示tscan和tphoto的主窗口&#xff0c;以及四个模块的主工具箱。 浏览点云 4、显示点云坐标信息(类&#xff0c; 航带号&#xff0c;GPS信息&#xff0c;东…

文心一言 VS 讯飞星火 VS chatgpt (67)-- 算法导论6.5 6题

文心一言 VS 讯飞星火 VS chatgpt &#xff08;67&#xff09;-- 算法导论6.5 6题 六、在 HEAP-INCREASE-KEY 的第 5 行的交换操作中&#xff0c;一般需要通过三次赋值来完成。想一想如何利用INSERTION-SORT 内循环部分的思想&#xff0c;只用一次赋值就完成这一交换操作? 文…

【Django+Vue】英文成绩管理平台--20230727

能够满足大部分核心需求&#xff08;标绿&#xff09;&#xff1a;报表部分应该比较难。 项目地址 前端编译 https://gitlab.com/m7840/toeic_vue_dist Vue源码 https://gitlab.com/m7840/toeic_vue Django源码 https://gitlab.com/m7840/toeic_python 项目架构 流程 …

嵌入式面试常见题目收藏(超总结)

​ 这篇文章来自很多博客主和其他网站的作者&#xff0c;如有侵权&#xff0c;联系必删 文章出处标注&#xff1a; https://blog.csdn.net/qq_44330858/article/details/128947083 ***如需PDF或者原稿可私信 *** ***如需PDF或者原稿可私信 *** ***如需PDF或者原稿可私信 *** 1.…

16位S912ZVML32F3MKH、S912ZVML31F1WKF、S912ZVML31F1MKH混合信号MCU,适用于汽车和工业电机控制应用。

S12 MagniV微控制器是易于使用且高度集成的混合信号MCU&#xff0c;非常适合用于汽车和工业应用。S12 MagniV MCU提供单芯片解决方案&#xff0c;是基于成熟的S12技术的完整系统级封装 (SiP) 解决方案&#xff0c;在整个产品组合内软件和工具都兼容。 S12 MagniV系统级封装 (S…

EP4CE6E22C8N Error: Can‘t recognize silicon ID for device 1

经过各种排查&#xff0c;发现是AS配置不对&#xff0c;仅供参考 工程 参考某处的工程画板配置的FPGA板子&#xff0c;用于学习入门FPGA。 烧录sof文件是正常的&#xff0c;并能正常运行。 但是烧录jic是failed&#xff0c;查看报错为&#xff1a;Error: Can’t recognize si…

【Maven】让maven更高效,优化maven构建项目速度

打开idea的setting&#xff0c;找到maven&#xff0c;设置它多线程数&#xff0c;重启后即可&#xff01; 我这里是8&#xff0c;你们可以随便设置。 如下图&#xff1a;

【高级数据结构】树状数组

目录 树状数组1 &#xff08;单点修改&#xff0c;区间查询&#xff09; 树状数组1 &#xff08;单点修改&#xff0c;区间查询&#xff09; 洛谷&#xff1a;树状数组1https://www.luogu.com.cn/problem/P3374 题目描述 如题&#xff0c;已知一个数列&#xff0c;你需要进行…

Vulnhub: shenron: 3靶机

kali&#xff1a;192.168.111.111 靶机&#xff1a;192.168.111.171 信息收集 端口扫描 nmap -A -sC -v -sV -T5 -p- --scripthttp-enum 192.168.111.171 修改hosts后访问目标80端口&#xff0c;发现是wordpress wpscan收集目标用户&#xff0c;爆破出密码&#xff1a;ilov…

前后端分离开发流程

1、介绍 在前后端分离开发中&#xff0c;前端负责用户界面和交互逻辑的实现&#xff0c;后端则处理业务逻辑和数据持久化。这种开发模式的优势在于前后端可以独立进行开发&#xff0c;提高了开发效率&#xff0c;并且使得前后端可以采用不同的技术栈来实现各自的功能。 2、开…

LabVIEW FPGA开发实时滑动摩擦系统

LabVIEW FPGA开发实时滑动摩擦系统 由于非线性摩擦效应的建模和补偿的固有困难&#xff0c;摩擦系统的运动控制已被广泛研究。最近&#xff0c;人们更加关注滑动动力学和滑动定位&#xff0c;作为传统机器人定位的低成本和更灵活的驱动替代方案。摩擦控制器设计和适当选择基础…

NodeJs后端项目使用docker打包部署

docker安装看之前的文章 默认已经安装好docker并且配置没有问题 拉取项目 https://gitee.com/coder-msc/docker-node 本地跑一个看看 pnpm install pnpm start 本地访问 http://localhost:1301/getname?name%E5%93%88%E5%88%A9%E6%B3%A2%E7%89%B9项目整个上传服务器 查看…

【Spring】Spring之循环依赖底层源码解析

什么是循环依赖 A依赖了B&#xff0c;B依赖了A。 示例&#xff1a; // A依赖了B class A{public B b; }// B依赖了A class B{public A a; }其实&#xff0c;循环依赖并不是问题&#xff0c;因为对象之间相互依赖是很正常的事情。示例&#xff1a; A a new A(); B b new B…

如何快速用PHP取短信验证码

要用PHP获取短信验证码&#xff0c;通常需要连接到一个短信服务提供商的API&#xff0c;并通过该API发送请求来获取验证码。由于不同的短信服务提供商可能具有不同的API和授权方式&#xff0c;我将以一个简单的示例介绍如何使用Go语言来获取短信验证码。 在这个示例中&#xff…

信驰达推出RTL8720DN系列2.4G和5G双频Wi-Fi+蓝牙二合一模块

近日&#xff0c;领先的无线物联网通信模块厂商深圳信驰达科技RF-star推出了基于RTL8720DN SoC的2.4 GHz和5 GHz双频Wi-Fi蓝牙二合一模块—RF-WM-20DNB1。 图 1信驰达RF-WM-20DNB1 Wi-Fi模块 RF-WM-20DNB1是一款低功耗单芯片无线蓝牙和Wi-Fi组合模块&#xff0c;支持双频(2.4 G…

php://filter绕过死亡exit

文章目录 php://filter绕过死亡exit前言[EIS 2019]EzPOP绕过exit 参考 php://filter绕过死亡exit 前言 最近写了一道反序列化的题&#xff0c;其中有一个需要通过php://filter去绕过死亡exit()的小trick&#xff0c;这里通过一道题目来讲解 [EIS 2019]EzPOP 题目源码&#…

IO进程线程第三天(7.31)time,localtime,文件io函数:open,umask,close,write,read,lseek,stat,

用read函数完成图片文件拷贝 #include<stdio.h> #include<head.h> int main(int argc, const char *argv[]) {//umask(0);//将文件权限掩码改为0&#xff0c;使得其他用户可写int fd open("/home/ubuntu/图片/2.jpg",O_RDONLY,0777);//打开图片if(fd&l…

Neo4j 集群和负载均衡

Neo4j 集群和负载均衡 Neo4j是当前最流行的开源图DB。刚好读到了Neo4j的集群和负载均衡策略&#xff0c;记录一下。 1 集群 Neo4j 集群使用主从复制实现高可用性和水平读扩展。 1.1 复制 集群的写入都通过主节点协调完成的&#xff0c;数据先写入主机&#xff0c;再同步到…
最新文章