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

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

目录

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

myarray.hpp

a.cpp 

一,STL初识

1.1 STL基本概念

1.2 vector 存放内置数据

1.3 vector存放自定义数据(及指针类型)

1.4 容器嵌套容器

二,STRING容器

2.1 构造函数

2.2 赋值操作

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.mzph.cn/diannao/7760.shtml

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

相关文章

JavaScript逆向技术

JavaScript逆向之旅&#xff1a;深入解析与实践 在数字时代&#xff0c;前端技术的迅速发展使得Web应用变得更加丰富和复杂。JavaScript&#xff0c;作为前端的核心语言&#xff0c;其安全性和隐私保护问题也逐渐浮出水面。JavaScript逆向&#xff0c;作为一种从前端代码中提取…

ros安装cartographer

安装 当然是先去看cartograpger官方文档了&#xff0c;照着说明一步步下来。 执行以下语句会报错&#xff0c; wstool merge -t src https://raw.githubusercontent.com/cartographer-project/cartographer_ros/master/cartographer_ros.rosinstall wstool update -t src参看…

c/c++:山顶元素

题目描述(题目链接) 从键盘输入一个整型二维数组&#xff0c;遍历二维数组中的每个元素&#xff0c;如果此元素比其上下左右的数字都大&#xff0c;即为山顶元素。 输入格式 一个≥1的整数&#xff0c;表名是几行几列的二维数组&#xff0c;一个整型二维数组&#xff0c;数据…

【数学】矩阵与矩阵乘法

矩阵 定义一个 n m n\times m nm 的矩阵如下&#xff1a; [ a 1 , 1 ⋯ a 1 , m ⋮ ⋱ ⋮ a n , 1 ⋯ a n , m ] \begin{bmatrix}a_{1,1}&\cdots&a_{1,m}\\\vdots&\ddots&\vdots\\a_{n,1}&\cdots&a_{n,m}\end{bmatrix} ​a1,1​⋮an,1​​⋯⋱⋯​…

java之continue语句

在java中&#xff0c;continue语句一般用在循环中&#xff0c;它的作用是结束本次循环&#xff0c;执行下一次循环。它在for循环用的比较多 下面是一段示例代码 public class Test {public static void main(String[] args) {int sum0;for(int i1;i<100;i){if(i%20){conti…

选择器、pxcook软件、盒子模型

结构伪类选择器 定义&#xff1a;根据结构的元素关系来查找元素。 <title>Document</title><style>li:first-child{color:aqua ;}li:last-child{color: aqua;}li:nth-child(3){color: aqua;}</style> </head> <body><ul><li>…

四川景源畅信:抖音的运营策略有哪些?

在数字营销的大潮中&#xff0c;抖音以其巨大的用户基础和强大的传播力成为众多品牌和商家的必争之地。那么&#xff0c;抖音的运营策略有哪些呢?这个问题涉及到内容创作、用户互动、数据分析和品牌合作等多个方面。 一、内容创作与优化在抖音&#xff0c;内容是吸引用户的关键…

有5个excel表,每个表有6列。用python把这5个表合成1个表。

要将五个Excel表格合并成一个表格&#xff0c;我们可以使用pandas库&#xff0c;它提供了一个简单且强大的方式来处理和分析数据。下面是一个步骤说明和示例代码&#xff1a; 步骤&#xff1a; 安装pandas和openpyxl&#xff08;如果你还没有安装的话&#xff09;&#xff1a…

【Transformer系列(5)】vision transformer(ViT)带来的思考?

一、ViT的意义 Vision Transformer&#xff08;ViT&#xff09;是一种基于Transformer架构的图像分类模型&#xff0c;它通过将图像划分为一系列的图像块&#xff08;patches&#xff09;&#xff0c;并将这些块转换为向量序列&#xff0c;然后通过Transformer的自注意力机制对…

【数据结构】C/C++ 带头双向循环链表保姆级教程(图例详解!!)

目录 一、前言 二、链表的分类 &#x1f95d;单链表 &#x1f95d;双链表 &#x1f95d;循环链表 &#x1f95d;带头双向循环链表 &#x1f34d;头节点&#xff08;哨兵位&#xff09;的作用 ✨定义&#xff1a; ✨作用&#xff1a; &#x1f347;总结 三、带头双向循环链表 …

[JUCE]从一个有关右值引用的bug,探幽移动语义

一、问题 当我尝试在\JUCE\extras\WindowsDLL\Builds\VisualStudio2022目录下编译JUCE库的时候&#xff0c;提示报错如下&#xff1a; 报错提示如下&#xff1a; 这里涉及到两个问题 一、这个std::move是干嘛用的 二、为什么这里会报错&#xff1f; 另外&#xff0c;我在实…

详细讲解lua中string.gsub的使用

string.gsub 是 Lua 标准库中的一个函数&#xff0c;用于全局替换字符串中的某些部分。string.gsub 是 Lua 中非常实用的一个函数&#xff0c;它可以用来进行字符串的处理和替换操作。 它的基本语法如下&#xff1a; string.gsub(s, pattern, replacement [, n])s 是要处理的…

题解:CF1954D(Colored Balls)

题解&#xff1a;CF1954D&#xff08;Colored Balls&#xff09; CF1954D&#xff0c;是 CodeForces 难得一见的“非多测”题目&#xff0c;我们来看一下。 题意简述&#xff1a;有 n n n 种不同的球&#xff0c;第 i i i 种球有 a i a_i ai​ 个&#xff08; 1 ≤ i ≤ n…

栈的磁盘优化:降低存取成本的算法与实现

栈的磁盘优化&#xff1a;降低存取成本的算法与实现 问题背景简单实现方法的分析实现方法PUSH操作POP操作成本分析渐近分析 优化实现方法实现方法成本分析渐近分析 进一步优化&#xff1a;双页管理策略实现方法管理策略成本分析 伪代码示例C代码示例结论 问题背景 在具有有限快…

【vulhub靶场】Tomcat中间件漏洞复现

【vulhub靶场】Tomcat中间件漏洞复现 一、Tomcat AJP 任意文件读取/包含漏洞 &#xff08;CVE-2020-1938&#xff09;1. 漏洞描述2. 影响版本3. 漏洞原理4. 漏洞复现 二、任意文件写入漏洞 &#xff08;CVE-2017-12615&#xff09;1. 漏洞原理2. 影响版本3. 漏洞复现 三、Tomca…

Java面试题:解释CountDownLatch, CyclicBarrier和Semaphore在并发编程中的使用

在并发编程中&#xff0c;CountDownLatch、CyclicBarrier 和 Semaphore 是 Java 提供的同步辅助类&#xff0c;它们用于控制线程之间的协调。以下是每个类的基本用法和特点&#xff1a; CountDownLatch&#xff08;倒计时门闩&#xff09;&#xff1a; CountDownLatch 是一个同…

C语言程序的编译与链接过程

在编写C语言程序时&#xff0c;我们通常只是编写源代码&#xff08;.c文件&#xff09;&#xff0c;但要让计算机真正执行这些代码&#xff0c;还需要经过编译和链接两个主要步骤。下面&#xff0c;我们将详细解析这两个过程。 一、编译过程 编译是将源代码&#xff08;.c文件…

unity华为sdk接入指路指南

目前比较靠谱的几个方案&#xff1a;试过几个仅供参考 温馨提示&#xff1a;最高目前可支持方案到unity2021版本以下&#xff0c;以上请联系华为官方寻求技术支持 Unity集成华为游戏服务SDK方式&#xff08;一&#xff09;&#xff1a;集成Unity官方游戏SDK&#xff1a; 华为…

数据倾斜常见的解决办法

造成数据倾斜的原因主要有2种&#xff1a;在map端读取不可分割的大文件、处理大量相同的key 解决办法一般有以下几种&#xff1a; 1、对于空值引发的数据倾斜 &#xff08;1&#xff09;在sql语句中单独处理空值的数据&#xff0c;最后union all &#xff08;2&#xff09;…

关于多态~

多态的基本概念 多态是c面向对象的三大特性之一 多态分为两类&#xff1a;静态多态和动态多态 静态多态&#xff1a;函数重载&#xff0c;运算符重载&#xff0c;复用函数名 动态多态&#xff1a;派生类和虚函数实现运行时多态 静态多态和动态多态的区别&#xff1a; 静态…