20240430,类模板案例-数组类封装,STL初识,STRING容器(构造函数,赋值)

我真的碎掉了,主要是我很缺那点钱啊现在,我真的碎掉了我碎掉了碎掉了碎掉了

0.8 类模板案例-数组类封装

 需求:1,存储:内置和自定义数据类型;
2,存到堆区;
3,构造函数传入数组容量;
4,拷贝函数,重载=,防止浅拷贝;
5,尾插法,尾删法;
6,通过下标访问数组元素;
7,获取数组元素个数和容量

AAAAAA:打印函数参数列表不能加CONST

myarray.hpp
//数组类
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class T>
class myArray {
public:
	//有参构造,容量
	myArray(int capacity) {
		//cout << "调用有参构造" << endl;
		this->m_capa = capacity;
		this->m_size = 0;
		this->m_Add = new T[this->m_capa];//开辟数组
	}
	//析构
	~myArray() {
		if (this->m_Add != NULL) {
			//cout << "调用  析构  hanshu1" << endl;
			delete[] this->m_Add;
			this->m_Add = NULL;
		}
	}
	//拷贝
	myArray(const myArray& arr) {
		this->m_size = arr.m_size;
		this->m_capa = arr.m_capa;
		this->m_Add = new T[this->m_capa];
		for (int i = 0; i < this->m_size; i++) {
			this->m_Add[i] = arr.m_Add[i];  
		}
		//cout << "调用  拷贝  hanshu1" << endl;
	}
	//operator=,返回自身的引用,做一个连等的操作
	myArray& operator = (const myArray & arr){
		//cout << "调用  重载  hanshu1" << endl;
		if (this->m_Add != NULL) {//先判断堆区是否有数据
			delete[] this->m_Add;
			this->m_Add = NULL;
			this->m_size = 0;
			this->m_capa = 0;
		}
		this->m_size = arr.m_size;
		this->m_capa = arr.m_capa;
		this->m_Add = new T[this->m_capa];
		for (int i = 0; i < this->m_size; i++) {
			this->m_Add[i] = arr.m_Add[i];  
		}
		return *this;
	}
	//尾插法
	void Push_back(const T& val) {
		if (this->m_size == this->m_capa) {//先判断容量
			return;
		}
		this->m_Add[this->m_size] = val;
		this->m_size++;
	}
	//尾删法
	void Pop_back() {
		if (this->m_size == 0) {//判断有无数据
			return;
		}
		this->m_size--;
	}
	//通过下标方式访问  重载[]  arr[0]=100作为左值存在,返回本身
	T& operator[](int index) {
		return this->m_Add[index];
	}
	//返回数组容量
	int getCapa() {
		return this->m_capa;
	}
	//返回数组大小
	int getSize() {
		return this->m_size;
	}
private:
	T* m_Add;//指针指向堆区开辟的数组
	int m_capa;//capacity 数组容量  总
	int m_size;//已有数据
};
a.cpp 
#include<iostream>
#include<string>
using namespace std;
#include "myarray.hpp"

void print1(myArray<int>& arr) {//这里不能加const,为毛
	for (int i = 0; i < arr.getSize(); i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}
class Person {
public:
	string m_name;
	int m_age;
	Person() {};
	Person(string name,int age) {
		this->m_name = name;
		this->m_age = age;
	}
	//~Person() {};
};
void print2(myArray<Person>& arr) {
	for (int i = 0; i < arr.getSize(); i++) {
		cout << arr[i].m_name << "\\"<<arr[i].m_age<<" ";
	}
	cout << endl;
}
void test01() {
	myArray<int> arr1(6);
	/*myArray<int> arr3(100);
	arr3 = arr1;*/
	for (int i = 0; i < 5; i++) {
		arr1.Push_back(i);//利用尾插法插入数据
	}
	cout << "arr1 的打印shuchu1" << endl;
	print1(arr1);
	cout << "arr1 的  容量 "<<arr1.getCapa() << endl;
	cout << "arr1 的  大小 " << arr1.getSize() << endl;
	myArray<int> arr2(arr1);
	cout << "arr2 的打印shuchu1" << endl;
	print1(arr2);
	arr2.Pop_back();
	print1(arr2);
	arr2.Push_back(89);
	print1(arr2);
	cout << arr2[3] << endl;
}
void test02() {
	myArray<Person> arr1(6);
	Person p1("士大夫", 34);
	Person p2("d发到网上夫", 34);
	Person p3("产生的VS1", 34);
	Person p4("放热峰", 34);
	Person p5("给夫", 34);
	Person p6("多穿点", 34);
	arr1.Push_back(p1);
	arr1.Push_back(p2);
	arr1.Push_back(p3);
	arr1.Push_back(p4);
	arr1.Push_back(p5);
	arr1.Push_back(p6);

	cout << "arr1 的打印shuchu1" << endl;
	print2(arr1);
	cout << "arr1 的  容量 " << arr1.getCapa() << endl;
	cout << "arr1 的  大小 " << arr1.getSize() << endl;
	myArray<Person> arr2(arr1);
	cout << "arr2 的打印shuchu1" << endl;
	print2(arr2);
	arr2.Pop_back();
	print2(arr2);
	arr2.Push_back(p1);
	print2(arr2);

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

一,STL初识

1.1 STL基本概念

STANDAR TEMPLATE LIBRARY标准模板库
广义上分为容器 CONTAINER ,算法ALGORITHM,迭代器ITERATOR,算法和容器之间通过迭代器无缝衔接
STL几乎所有的代码都采用了模板类或者函数模板

六大组件:容器,算法,迭代器,仿函数,适配器(配接器),空间配置器
容器:各种数据结构,vector ,list ,deque ,set ,map 等
算法:各种常用的算法,SORT,COPY,FIND,FOR_EACH等
迭代器:扮演了容器与算法之间的胶合剂
仿函数:行为类似函数,可以作为算法的某种策略
适配器:一种用来修饰容器或者仿函数或迭代器接口的东西
空间配置器:负责空间的配置与管理

容器:序列式【强调值的排序】,关联式【二叉树结构,顺序之间没有严格意义上的顺序关系】
算法:质变,非质变(Algorithms)
迭代器:提供一种专属方法,能依序访问容器元素,不暴露容器内部表达方式,类似指针
种类:
输入   只读                           只读,支持++,==,!=
输出   只写                           只写,支持++
前向   读写,能向前推进      读写,支持++,==,!=
双向   读写,向前&向后       读写,支持++,--
随机   读写,跳跃-》任意     读写,支持++,--,[n],-n,<,<=, >, >=

1.2 vector 存放内置数据

 算法:for_each,迭代器:vector<int>::iterator
第三种本质上是一种回调函数?感觉不难懂但是好像没有很懂……

#include<iostream>
using namespace std;
#include <vector>
#include<algorithm>

void myprint(int bal) {
	cout << bal << " ";
}
void test01() {
	vector<int> v;//创建容器
	v.push_back(10);//插入数据
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);
	//通过迭代器访问
	vector<int>::iterator itBegin = v.begin();//起始迭代器  指向第一个
	vector<int>::iterator itEnd = v.end();//结束迭代器  指向最后一个的下一个
	while (itBegin != itEnd) {
		cout << *itBegin << " ";
		itBegin++;
	}
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << endl;
	}
	//需要包含算法头文件
	for_each(v.begin(), v.end(), myprint);
}
int main() {
	test01();
	system("pause");
	return 0;
}
1.3 vector存放自定义数据(及指针类型)
#include<iostream>
using namespace std;
#include <vector>
#include<string>

class Person {
public:
	Person(string name, int age) {
		this->_name = name;
		this->_age = age;
	}
	string _name;
	int _age;
};
//存放自定义数据类型
void test01() {
	vector<Person>v;
	Person p1("士大夫", 34);
	Person p2("d发到网上夫", 3);
	Person p3("产生的VS1", 4);
	Person p4("放热峰", 88);
	Person p5("给夫", 9);
	Person p6("多穿点", 13);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	v.push_back(p6);
	for (vector<Person>::iterator i = v.begin(); i != v.end(); i++) {
		cout << "xingm:" << i->_name << "\tage:" << i->_age<<"\t\t\t";
		cout << "xingm:" << (*i)._name << "\tage:" << (*i)._age << endl;
	}
}
//存放自定义数据指针类型
void test02() {
	vector<Person*>v;
	Person p1("士大夫", 34);
	Person p2("d发到网上夫", 3);
	Person p3("产生的VS1", 4);
	Person p4("放热峰", 88);
	Person p5("给夫", 9);
	Person p6("多穿点", 13);
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);
	v.push_back(&p6);
	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
		cout  << (*it)->_name << "\tage:" << (*it)->_age << endl;
	}
}
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}
1.4 容器嵌套容器

 星括号里面是什么类型,解出来就是什么类型

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

class Person {
public:
	Person(string name, int age) {
		this->_name = name;
		this->_age = age;
	}
	string _name;
	int _age;
};
//存放自定义数据类型
void test01() {
	vector<vector<int>> v;//嵌套容器
	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	vector<int>v4;
	for (int i = 0; i < 4; i++) {
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);
	for (vector<vector<int>>::iterator it = v.begin();it != v.end(); it++) {
		//(*it)-----容器vector<int>
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
			cout << *vit << " ";
		}
		cout << endl;
	}
}
int main() {
	test01();
	system("pause");
	return 0;
}

二,STRING容器

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

2.1 构造函数

string();                          创建一个空的字符串
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>


void test01() {
	string s1;//创建一个空的字符串
	const char* str = "hello world";

	string s2(str);           //使用字符串S初始化
	cout << "string1:" << s1 << endl;
	cout << "string2:" << s2 << endl;

	string s3(s2);           // 使用一个STRING对象初始化另一个STRING对象
	cout << "string3:" << s3 << endl;

	string s4(10,'a');       //使用n个字符C初始化
	cout << "string4:" << s4 << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}
2.2 赋值操作

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       赋值给当前的字符串 

#include<iostream>
using namespace std;
#include <vector>
#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 c);               //用N个字符C       赋值给当前的字符串
*/
void test01() {
	string str1;
	str1 = "hello world";    //string& operator=(const char* s);
	cout << "str1:\t"<<str1 << endl;

	string str2;
	str2 = str1;             //string& operator=(const string& s);
	cout << "str2:\t" << str2<< endl;

	string str3;
	str3 = 'f';              //string& operator=(char c); 
	cout << "str3:\t" << str3 << endl;

	string str4;
	str4.assign("dfws");     //string& assign(const char *s); 
	cout << "str4:\t" << str4 << endl;

	string str5;
	str5.assign("fwefrwegaqr3t", 7);      //string& assign(const char*s,int n);
	cout << "str5:\t" << str5 << endl;

	string str6;
	str6.assign(str5);       //string& assign(int n,char c); 
	cout << "str6:\t" << str6 << endl;

	string str7;
	str7.assign(18,'f');       //string& assign(const string &s); 
	cout << "str7:\t" << str7 << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

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

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

相关文章

Docker部署RabbitMQ与简单使用

官网地址&#xff1a; Messaging that just works — RabbitMQ 我的Docker博客:Docker-CSDN博客 1.结构 其中包含几个概念&#xff1a; **publisher**&#xff1a;生产者&#xff0c;也就是发送消息的一方 **consumer**&#xff1a;消费者&#xff0c;也就是消费消息的一方 …

第三节课,功能2:开发后端用户的管理接口--http client -- debug测试

一、idea 中 Http client 使用 二、测试步骤&#xff0c;先进入主程序 2.1 先run &#xff0c;再debug 2.2 再进入想要测试的代码 2.2.1 进入测试的接口 三、程序逻辑 1&#xff09;用户注册逻辑&#xff1a;如果用户不存在再后端&#xff0c;看用户名&密码&校验码是…

一些优雅的监控运维技巧

准备工作 安装 sysstat sudo apt install sysstat查看某个进程的cpu情况 pidstst -u -p 256432查看某个进程的RAM情况 pidstst -r -p 256432查看某个进程的IO情况 pidstst -d -p 256432查看某个进程下的线程执行情况 pidstst -t -p 256432查看指定PID的进程对应的可执行文件…

ip ssl证书无限端口网站

IP SSL证书是由CA认证机构颁发的一种特殊数字证书。大部分SSL数字证书都需要用户使用域名进行申请&#xff0c;想要对公网IP地址加密实现https访问就需要申请IP SSL证书。IP SSL证书采用了强大的加密算法&#xff0c;可以有效地防止数据在传输过程中被窃取或篡改&#xff0c;具…

nbcio-boot基于jeecg的flowable支持部门经理的单个或多实例支持(前端部分)

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a;RuoYi-Nbcio后台管理系统 http://218.75.87.38:9666/ 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; h…

特斯拉与百度合作;字节正全力追赶AI业务;小红书内测自研大模型

特斯拉中国版 FSD 或与百度合作 根据彭博社的报道&#xff0c;特斯拉将通过于百度公司达成地图和导航协议&#xff0c;扫清在中国推出 FSD 功能的关键障碍。 此前&#xff0c;中国汽车工业协会、国家计算机网络应急技术处理协调中心发布《关于汽车数据处理 4 项安全要求检测情…

大数据组件之Storm详解

Storm 是一个免费并开源的分布式实时计算系统&#xff0c;具有高容错性和可扩展性。它能够处理无边界的数据流&#xff0c;并提供了实时计算的功能。与传统的批处理系统相比&#xff0c;Apache Storm 更适合处理实时数据。 让我们深入了解一下 Storm&#xff1a; 1.Storm 简介…

Systemback Ubuntu14.04 制作自定义系统ISO镜像

工作需要&#xff0c;要基于ubuntu自定义一些编译环境并将自己配置好的ubuntu做成镜像。 硬件准备 ​ 为保证能够顺利完成系统iso镜像的制作与系统还原&#xff0c;推荐准备一个较大容量的U盘或者移动固态硬盘&#xff0c;同时确保自己的Ubuntu系统还有比较大的可用空间。 1 S…

sgg_ssm学习--前端搭建遇到的问题

目录 问题1&#xff1a;由于我是解压缩软件nodejs&#xff0c;没有添加系统路径 解决&#xff1a;添加nodejs的路径 到系统 path中 问题2&#xff1a;vscode 终端输入npm命令 报错 解决(如图所示在vscode打开前端工程&#xff0c;终端修改如下配置)&#xff1a; 问题1&…

CSS 伪类、伪元素的应用实例:电池充电、高能进度条

一、目的 本文通过 CSS 伪类、伪元素&#xff0c;结合动画 animation 和 Vue 动态样式属性&#xff08;通过 CSS 变量&#xff09;的写法&#xff0c;来实现电池充电、高能进度条的效果&#xff0c;如下图所示。 二、基础知识 1、CSS 伪类、伪元素 简单概括成以下 4 点&#x…

如何提升制造设备文件汇集的可靠性和安全性?

制造设备文件汇集通常指的是将与制造设备相关的各种文档和资料进行整理和归档的过程。这些文件可能包括但不限于&#xff1a; 生产数据&#xff1a;包括生产计划、订单信息、生产进度等。 设计文件&#xff1a;如CAD图纸、设计蓝图、产品模型等。 工艺参数&#xff1a;用于指…

新唐的nuc980/nuc972的开发1-环境和源码同步

开发环境安装 1.1更新源 服务器端&#xff1a;可以参考&#xff1a;Linux替换清华源_更改清华源-CSDN博客 下面是桌面端的方法&#xff1a; 打开系统的软件中心&#xff0c;选择自己想要使用的源 更新缓存 1.2安装必须的库 apt-get install patch apt-get install libc6-dev …

ClickHouse高原理与实践

ClickHouse高原理与实践 1 ClickHouse的特性1.1. OLAP1.2. 列式存储1.3. 表引擎1.4. 向量化执行1.5. 分区1.6. 副本与分片1.7 其他特性 2. ClickHouse模块设计2.1 Parser分析器与Interpreter解释器2.2 Storage2.3 Column与Field2.4 DataType2.5 Block2.6 Cluster与Replication …

数据库基础--MySQL简介以及基础MySQL操作

数据库概述 数据库&#xff08;DATABASE&#xff0c;简称DB&#xff09; 定义:是按照数据结构来组织、存储和管理数据的仓库.保存有组织的数据的容器(通常是一个文件或一组文件) 数据库管理系统(Database Management System,简称DBMS) 专门用于管理数据库的计算机系统软件;…

机器学习:深入解析SVM的核心概念【一、间隔与支持向量】

直接阅读原始论文可能有点难和复杂&#xff0c;所以导师直接推荐我阅读周志华的《西瓜书》&#xff01;&#xff01;然后仔细阅读其中的第六章&#xff1a;支持向量机 间隔与支持向量 **问题一&#xff1a;什么叫法向量&#xff1f;为什么是叫法向量**什么是法向量&#xff1f;…

c#数据库: 10.调用存储过程查询信息,并显示在窗体上

查询女生信息&#xff0c;并将信息显示在窗体上: 原数据表//右键数据库名,新建查询 ------------- 新建查询窗口,添加新建存储过程Procedure_GetGirls1和查询代码如下 : CREATE PROCEDURE dbo.Procedure_GetGirls1 /*存储过程名称*/ AS SELECT * f…

如何通过前后端交互的方式制作Excel报表

前言 Excel拥有在办公领域最广泛的受众群体&#xff0c;以其强大的数据处理和可视化功能&#xff0c;成了无可替代的工具。它不仅可以呈现数据清晰明了&#xff0c;还能进行数据分析、图表制作和数据透视等操作&#xff0c;为用户提供了全面的数据展示和分析能力。 今天小编就…

2024年武汉东湖高新水测成绩出来了

本次水测通过人员有1016名&#xff0c;通过的人数还是蛮多的&#xff0c;水测其实没有大家想象的那么难&#xff0c;现在职称评审都是水测线下评审的模式进行的。 水平测试分机考&#xff0c;笔试和面试答辩&#xff0c;各区随机安排选其一&#xff0c;机考就相当于考驾照刷题&…

HTML:元素分类

HTML&#xff1a;元素分类 概述块级元素&#xff08;Block-level Elements&#xff09;内联元素&#xff08;Inline Elements&#xff09;替换元素&#xff08;Replaced Elements&#xff09;表单元素&#xff08;Form Elements&#xff09; 概述 HTML&#xff08;HyperText M…

如何使用Spring Boot导出数据到Excel表格

在开发应用程序时&#xff0c;经常会遇到将数据导出到Excel表格的需求。Spring Boot提供了简单而有效的方法来实现这个功能。本文将介绍如何使用Spring Boot和Apache POI库将数据导出到Excel表格&#xff0c;并提供一个示例代码来演示该过程。 1. 准备工作 首先&#xff0c;确…
最新文章