栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 提高编程阶段 模板 STL初识 string容器 vector容器 deque容器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 提高编程阶段 模板 STL初识 string容器 vector容器 deque容器

本阶段主要针对
C++泛型编程和STL技术做详细讲解,探讨c++更深层的使用
模板-模板的概念   
(模板就是建立通用的模具,大大提高复用性)
模板-函数模板基本语法 

模板-函数模板注意事项 

模板-函数模板案例-数组排序 
模板-普通函数与函数模板区别

模板-普通函数与函数模板调用规则 

模板-模板的局限性

模板-类模板基本语法 

模板-类模板与函数模板区别 

模板-类模板中成员函数创建时机 

模板-类模板对象做函数参数 

模板-类模板与继承
如果父类是类模板,子类继承的时候需要指定父类中T的数据类型

模板-类模板成员函数类外实现 
学习目标:掌握类模板中的成员函数类外实现

模板-类模板分文件编写


模板-类模板与友元

模板-类模板案例-数组类封装的需求分析

模板-类模板案例-数组类封装(上)     模板-类模板案例-数组类封装(下)
//自己通用的数组类 .hpp
#pragma once
#include 
using namespace std;

template 
class MyArray {
public:
	//有参构造 参数 容量
	MyArray(int capacity)
	{
		//cout << "Mtarray有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];

	}
	//拷贝构造 防止浅拷贝
	MyArray(const MyArray& arr) {
		//cout << "Mtarray拷贝构造调用" << endl;

		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;  浅拷贝指针直接赋值 导致堆区数据重复放置

		//深拷贝
		this->pAddress=new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator= 放止浅拷贝问题  
	MyArray& operator=(const MyArray& arr)
	{
		//cout << "Mtarray 的 operator调用" << endl;
		//先判断原来堆区是否有数据, 如果有先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress= NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//尾插法
	void Push_Back(const T & val) {
		if (this->m_Capacity == this->m_Size)//判满
		{
			return ;
		}
		this->pAddress[this->m_Size] = val; //在数组末尾插入数据
		this->m_Size++;//更新数组大小

	}
	//尾删法
	void Pop_Back(MyArray& arr) {
		//让用户访问不到最后一个元素,即为尾删,逻辑删除

		if (this->m_Size==0)//判空
		{
			return;
		}
		this->m_Size--;//更新数组大小
	}

	//通过下标的方式访问数组中的元素  arr[0]
	T&  operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组容量
	int getCap()
	{
		return this->m_Capacity;
	}
	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}

	//析构函数
	~MyArray()
	{
		if (this->pAddress != NULL) {
		//	cout << "Mtarray析构调用" << endl;
			delete[] this->pAddress;
			this->pAddress=NULL;
		}
	}
private:
	T* pAddress; //指针指向堆区开辟的真实数组

	int m_Capacity;//数组容量
	int m_Size; //数组大小

};


//----------------------------------
#include 
using namespace std;
#include "MyArray.hpp"
#include 

void printIntArray(MyArray & arr) {

	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << endl;
	}
}
void test01()
{
	MyArray arr1(5);
	
	for (int i = 0; i < 5; i++)
	{
		arr1.Push_Back(i);//尾插法插入数组
	}
	cout << "arr1的打印输出:" << endl;
	printIntArray(arr1);

	cout << "arr1的容量:" < arr2(arr1);
	
	arr2.Pop_Back(arr2);
	cout << "arr2的容量:" << arr2.getCap() << endl;
	cout << "arr2的大小:" << arr2.getSize() << endl;
	printIntArray(arr2);

}

//测试自定义数据类型
class Person
{
public:
	Person() {};
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};
void printPersonArray(MyArray & arr) {

	for (int i = 0; i < arr.getSize(); i++)
	{
		cout <<"姓名: "<< arr[i].m_Name<<"年龄: "< arr(10);

	Person p1  ("孙悟空", 999);
	Person p2  ("韩信",30 );
	Person p3  ("妲己", 20); 
	Person p4  ("赵云", 25);
	Person p5  ("安其拉", 27);

	//将数据插入到数组
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);
	printPersonArray(arr);

	cout << "arr的容量:" << arr.getCap() << endl;
	cout << "arr的大小:" << arr.getSize() << endl;
}

int main()
{
	test02();

	system("pause");
	return 0;
}
STL初识-STL的基本概念


常用容器中 迭代器种类为双向迭代器和随机访问迭代器
STL初识-Vector存放内置数据类型


STL初识-Vector存放自定义数据类型
STL初识-容器嵌套容器
string容器-构造函数

string容器-赋值操作
 
string容器-字符串拼接

string容器-字符串查找和替换

string容器-字符串比较

string容器-字符存取
string容器-字符串插入和删除
string容器-子串获取   (pos,npos); 包左不包右

vector容器-构造函数    (单端数组) 最常用的容器之一


vector容器-赋值操作

vector容器-容量和大小

vector容器-插入和删除
vector容器-数据存取

vector容器-互换容器

vector容器-预留空间


deque容器-构造函数    双端数组  访问慢 删改快  迭代器也支持随机访问


deque容器-赋值操作

deque容器-大小操作

deque容器-插入和删除

deque容器-数据存取

deque容器-排序操作

STL 案例1-评委打分
#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 

class Person {//选手类
public:
	Person(string name, int score) {
		this->m_Name = name;
		this->m_Score = score;
	}

	string m_Name;
	int m_Score;
};

void createPerson(vector& v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += nameSeed[i];

		int score = 0;

		Person p(name, score);

		//将创建的person对象 放入到容器中
		v.push_back(p);

	}
}

void setScore(vector& v) {
	for (vector ::iterator it = v.begin(); it != v.end(); it++)
	{
		//将评委的分数 放入到 deque容器
		deque  d;
		
		for (int i = 0; i < 10; i++) {
			int score =rand() %41 +60 ;//60~100的随机打分
			d.push_back(score);
		}
		//cout << "选手: " << it->m_Name << "打分: " << endl;
		//for (deque::iterator dit = d.begin(); dit != d.end(); dit++)
		//{
			//cout<< *dit<<" ";
		//}cout<< endl;

		//平均分 取最高最低
		sort(d.begin(), d.end());
		d.pop_back();
		d.pop_front();

		//取平均数
		int sum = 0;
		for (deque::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;
		}
		int avg = sum / d.size();

		//将平均分赋值
		it->m_Score = avg;
		
	}
}

void showScore(vector& p) {
	for (vector::iterator it = p.begin(); it != p.end(); it++) {
		cout << "选手: " << it->m_Name << "平均分: " <m_Score<< endl;
	}

}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));
	//1.创建5名选手
	vector v;
	createPerson(v);

	//for (vector ::iterator it = v.begin(); it != v.end(); it++)
	//{
	//		cout << "姓名: " << (*it).m_Name << "分数: " << (*it).m_Score << endl;
	//}

	//2.给5名选手打分
	setScore(v);
	
	//3.显示最后得分
	showScore(v);
	system("pause");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号