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;
}
  • 30
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值