C++学习系列(三)—— 泛型编程(STL)

原文链接:https://www.wkeyu.cn/235.html

C++提高编程

本阶段主要针对C++泛型编程 和***STL*** 技术做详细学习,学习C++更深层次的使用
代码仓库:https://github.com/Kerry-yu/Cpp_Learn

1. 模板

1.1 模板的概念

模板就是建立通用的模具 ,大大提高复用性

例如生活中的模板——PPT模板

模板的特点:

  • 模板不可以直接使用,他只是一个框架
  • 模板的通用并不是万能的

1.2 函数模板

  • C++ 提供另一种编程思想称为 泛型编程 ,主要利用的技术就是模板
  • C++ 提供两种模板机制:函数模板类模板
1.2.1 函数模板语法

函数模板作用:

建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。

语法:

template<typename T>
函数声明或定义

解释:

template —— 声明创建模板

typename —— 表明其后面的符号是一种数据类型,可以用class代替

T —— 通用的数据类型,名称可以替换,通常为大写字母

示例:

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

//函数模板

//交换两个整型
void swapInt(int& a, int& b)
{
   
	int temp = a;
	a = b;
	b = temp;
}

//交换两个浮点型
void swapDouble(double& a, double& b)
{
   
	double temp = a;
	a = b;
	b = temp;
}

//函数模板
template<typename T>	//声明一个模板,T是一个通用数据类型

void mySwap(T &a, T &b)
{
   
	T temp = a;
	a = b;
	b = temp;
}

void test01()
{
   
	int a = 10;
	int b = 20;

	//swap(a, b);

	//利用函数模板交换
	//两种方式使用函数模板
	//1. 自动数据类型推导
	mySwap(a, b);
	//2. 显示指定类型
	mySwap<int>(a, b);

	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

	double c = 1.1;
	double d = 2.2;

	swap(c, d);

	cout << "c=" << c << endl;
	cout << "d=" << d << endl;

}

int main()
{
   
	test01();

	return 0;
}

总结:

  • 模板利用关键字 template
  • 使用函数模板有两种方式:自动类型推导、显示指定类型
  • 模板的目的是为了提高复用性,将类型参数化
1.2.2 函数模板注意事项

注意事项:

  • 自动类型推导,必须推导出一致的数据类型T,才可以使用
  • 模板必须要确定出T的数据类型,才可以使用

示例:

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

//函数模板注意事项



template<typename T>	//typename可以替换为class
void mySwap(T& a, T& b)
{
   
	T temp = a;
	a = b;
	b = temp;
}

void test01()
{
   
	int a = 10;
	int b = 20;
	char c = 'c';

	//1. 自动类型推导,必须推导出一致的数据类型T,才可以使用
	mySwap(a, b);//一致,正确
	//mySwap(a, c);//不一致,错误

	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

//2. 模板必须要确定出T的数据类型,才可以使用
template<typename T>
void func()
{
   
	cout << "func 调用" << endl;
}

void test02()
{
   
	//func();//未确定T数据类型,不能调用
	func<int>();
}

int main()
{
   
	test01();
	test02();

	return 0;
}
1.2.3 函数模板案例

案例描述:

  • 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
  • 排序规则从小到大,排序算法为选择排序
  • 分别利用char数组int数组进行测试

示例:

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

//实现通用	对数组进行排序的函数
//规则	从大到小
//算法	选择
//测试	char数组	、int数组

//交换函数模板
template<typename T>
void mySwap(T& a, T& b)
{
   
	T temp = a;
	a = b;
	b = temp;
}

//排序算法
template<typename T>
void mySort(T arr[], int len)
{
   
	for (int i = 0; i < len; i++)
	{
   
		int max = i;//认定最大值下标
		for (int j = i + 1; j < len; j++)
		{
   
			//认定的最大值比遍历出的数值要小,说明 j下标的元素才是真正的最大值
			if (arr[max] < arr[j])
			{
   
				max = j;//更新最大值下标
			}
		}
		if (max != i)
		{
   
			//交换max和i元素
			mySwap(arr[max], arr[i]);
		}
	}
}

//打印数组模板
template<typename T>
void printArray(T arr[], int len)
{
   
	for (int i = 0; i < len; i++)
	{
   
		cout << arr[i] << " ";
	}
	cout << endl;
}

//测试char数组
void test01()
{
   
	char charArr[] = "bacfe";
	int num = sizeof(charArr) / sizeof(char);

	mySort(charArr, num);
	printArray(charArr, num);
}

//测试int数组
void test02()
{
   
	int intArr[] = {
    7,5,8,6,9,15,2,4 };
	int num = sizeof(intArr) / sizeof(int);

	mySort(intArr, num);
	printArray(intArr, num);
}
int main()
{
   
	test01();
	test02();

	return 0;
}

image-20211101163353672

1.2.4 普通函数和函数模板的区别

普通函数和函数模板的区别:

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换

示例:

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

//普通函数与函数模板的区别

//普通函数 
int myAdd01(int a, int b)
{
   
	return a + b;
}

//函数模板
template<typename T>
T myAdd02(T a, T b)
{
   
	return a + b;
}

void test01()
{
   
	int a = 10;
	int b = 20;
	char c = 'c';

	cout << myAdd01(a, b) << endl;
	//普通函数会发生隐式类型转换
	cout << myAdd01(a, c) << endl;

	//函数模板使用自动类型推导时 不会发生隐式类转换
	//cout << myAdd02(a, c) << endl;

	//利用显示指定类型的方式,可以发生隐式类型转换
	cout << myAdd02<int>(a, c) << endl;
}
int main()
{
   
	test01();

	return 0;
}

image-20211101164211535

总结:建议使用显示指定类型的方式,调用函数模板;因为可以自己确定通用类型T

1.2.5 普通函数与函数模板的调用规则

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数
  2. 但可以通过空模板参数列表来强制调用函数模板
  3. 函数模板也可以发生重载
  4. 如果函数模板可以产生更好的匹配,优先调用函数模板
#include<iostream>
#include<string>
using namespace std;

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

void myPrint(int a, int b)
{
   
	cout << "调用普通函数" << endl;
}

template<typename T>
void myPrint(T a, T b)
{
   
	cout << "调用函数模板" << endl;
}
//函数模板重载
template<typename T>
void myPrint(T a, T b, T c)
{
   
	cout << "调用重载的函数模板" << endl;
}

void test01()
{
   
	int a = 10;
	int b = 10;
	int c = 10;

	//1. 如果函数模板和普通函数都可以实现,优先调用普通函数
	myPrint(a, b);

	//2. 可以通过空模板参数列表 来强制调用函数模板
	myPrint<>(a, b);

	//3. 函数模板也可以发生重载
	myPrint(a, b, c);

	//4. 如果函数模板可以产生更好的匹配,优先调用函数模板
	char c1 = 'a';
	char c2 = 'b';
	myPrint(c1, c2);
}
int main()
{
   
	test01();

	return 0;
}

image-20211101165351387

总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性

1.2.6 模板的局限性

局限性: 模板的通用性并不是万能的

例如:

template<typename T>
void f(T a, T b)
{
   
    a = b;
}

在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

再例如:

template<typename T>
void f(T a, T b)
{
   
    if(a > b){
    ... }
}

在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行


因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

示例:

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

//模板的局限性
//模板并不是万能的,有些特定数据类型,需要用具体化的方式做特殊实现

class Person
{
   
public:
	Person(string name, int age)
	{
   
		this->m_Name = name;
		this->m_Age = age;
	}

public:
	string m_Name;
	int m_Age;

};


//对比两个数据是否相等函数
template<typename T>
bool myCompare(T& a, T& b)
{
   
	if (a == b)
	{
   
		return true;
	}
	else
	{
   
		return false;
	}
}

//利用具体化的Person版本实现
template<> bool myCompare(Person& p1, Person& p2)
{
   
	if (p1.m_Name == p2.m_Name)
	{
   
		return true;
	}
	else
	{
   
		return false;
	}
}

//输出数据是否相等
template<typename T>
void myPrint(T a, T b)
{
   
	bool ret = myCompare(a, b);

	if (ret)
	{
   
		cout << "a == b" << endl;
	}
	else
	{
   
		cout << "a != b" << endl;
	}
}

void test01()
{
   
	int a = 10;
	int b = 20;

	myPrint(a, b);
}

void test02()
{
   
	Person a("Tom", 10);
	Person b("Tom", 10);

	myPrint(a, b);
}

int main()
{
   
	test01();
	test02();

	return 0;
}

image-20211101171128749

总结:

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

1.3 类模板

1.3.1 类模板语法

类模板作用: 建立一个通用类,类中的成员、数据类型可以不具体制定,用一个虚拟的类型来代表

语法:

template<class T>

解释:

template —— 声明创建模板

typename —— 表明其后面的符号是一种数据类型,可以用class代替

T —— 通用的数据类型,名称可以替换,通常为大写字母

示例:

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

//类模板
template<class NameType, class AgeType>
class Person
{
   
public:
	Person(NameType name, AgeType age)
	{
   
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
   
		cout << "name:" << this->m_Name << endl;
		cout << "age:" << this->m_Age << endl;
	}

public:
	NameType m_Name;
	AgeType m_Age;
};

void test01()
{
   
	Person<string, int> p1("张三", 20);
	p1.showPerson();
}
int main()
{
   
	test01();

	return 0;
}

总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

1.3.2 类模板与 函数模板的区别

类模板与函数模板区别主要有两点:

  1. 类模板没有自动类型推导的使用方式
  2. 类模板在模板参数列表中可以有默认参数

示例:

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

//类模板与函数模板区别
//template<class NameType,class AgeType>
//模板参数列表可以包含默认参数
template<class NameType, class AgeType = int>
class Person
{
   
public:
	Person(NameType name, AgeType age)
	{
   
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
   
		cout << "name:" << this->m_Name << endl;
		cout << "age:" << this->m_Age << endl;
	}

public:
	NameType m_Name;
	AgeType m_Age;
};

//1. 类模板没有自动类型推导使用方式
void test01()
{
   
	//Person p("张三",18);//错误,无法使用自动类型推导
	Person<string, int> p("张三", 18);
	p.showPerson();
}

//2. 类模板在模板参数列表中可以有默认参数
void test02()
{
   
	Person<string> p("李四", 18);
	p.showPerson();
}


int main()
{
   
	test01();
	test02();

	return 0;
}

image-20211101190422020

总结:

  • 类模板使用只能用显示指定类型方式
  • 类模板中的模板参数列表可以有默认参数
1.3.3 类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建

示例:

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

//类模板中成员函数创建时机
//类模板中成员函数在调用时才去创建

class Person1
{
   
public:
	void showPerson1()
	{
   
		cout << "Person1 show" << endl;
	}
};

class Person2
{
   
public:
	void showPerson2()
	{
   
		cout << "Person2 show" << endl;
	}
};

template<class T>
class MyClass
{
   
public:
	//类模板中的成员函数
	void func1()
	{
   
		obj.showPerson1();
	}

	void func2()
	{
   
		obj.showPerson2();
	}
public:
	T obj;
};

void test01()
{
   
	MyClass<Person1> m;
	m.func1();
	//m.func2();
}

int main()
{
   
	test01();

	return 0;
}

总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建

1.3.4 类模板对象做函数参数

学习目标: 类模板实例化出的对象,向函数传参的方式

一共有三种传入方式:

  1. 指定传入的类型 —— 直接显示对象的数据类型
  2. 参数模块化 —— 将对象中的参数变为模板进行传递
  3. 整个类模板化 —— 将这个对象模型 模板化进行传递

示例:

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

//类模板对象做函数参数
template<class T1, class T2>
class Person
{
   
public:
	Person(T1 name, T2 age)
	{
   
		this->m_Name = name;
		this->m_Age = age;
	}
	void showPerson()
	{
   
		cout << "姓名:" << this->m_Name << endl;
		cout << "年龄:" << this->m_Age << endl;
	}

public:
	T1 m_Name;
	T2 m_Age;
};

//1. 指定传入方式
void printPerson(Person<string, int>& p)
{
   
	p.showPerson();
}

void test01()
{
   
	Person<string, int> p("张三", 18);
	printPerson(p);
}

//2. 参数模板化
template<class T1, class T2>
void printPerson2(Person<T1, T2>& p)
{
   
	p.showPerson();

	//查看参数数据类型
	cout << "T1的类型为:" << typeid(T1).name() << endl;
	cout << "T2的类型为:" << typeid(T2).name() << endl;
}

void test02()
{
   
	Person<string, int> p("李四", 20);
	printPerson2(p);
}

//3. 整个类模板化
template<class T>
void printPerson3(T& p)
{
   
	p.showPerson();
}

void test03()
{
   
	Person<string, int> p("王五", 40);
	printPerson3(p);
}

int main()
{
   
	test01();
	test02();
	test03();

	return 0;
}

image-20211101193204987

1.3.5 类模板与继承

当类模板碰到继承时,需要注意以下几点:

  • 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模板

示例:

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

//类模板与继承
template<class T>
class Base
{
   
public:
	T m;
};

//1. 必须要知道父类的T类型,才能继承给子类
//class Son :public Base
class Son :public Base<int>
{
   

};

void test01()
{
   
	Son s1;
}

//2. 如果想灵活指定父类的T类型,子类也需要变为类模板
template<class T1, class T2>//父类为T1,子类为T2
class Son2 :public Base<T2>
{
   
public:
	Son2()
	{
   
		cout << "T1的类型为:" << typeid(T1).name() << endl;
		cout << "T2的类型为:" << typeid(T2).name() << endl;
	}
public:
	T1 obj;
};

void test02()
{
   
	Son2<int, char> s2;
}

int main()
{
   
	test01();
	test02();

	return 0;
}

image-20211101194257998

总结:如果父类是类模板,子类需要指定出父类中T的数据类型

1.3.6 类模板成员函数的类外实现

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

示例:

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

//类模板成员函数类外实现
template<class T1, class T2>
class Person
{
   
public:
	Person(T1 name, T2 age);

	void showPerson();

public:
	T1 m_Name;
	T2 m_Age;
};

//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
   
	this->m_Name = name;
	this->m_Age = age;
}

//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
   
	cout << "姓名:" << this->m_Name << endl;
	cout << "年龄:" << this->m_Age << endl;
}

void test01()
{
   
	Person<string, int> p("张三", 18);
	p.showPerson();
}

int main()
{
   
	test01();

	return 0;
}

总结:类模板中成员函数类外实现时,需要加上模板参数列表

1.3.7 类模板分文件编写

学习目标: 掌握类模板成员函数分文件编写产生的问题以及解决方式

问题: 类模板中成员函数创建时机是在调用阶段,导致份文件编写时链接不到

解决:

  • 解决方式1:直接包含.cpp源文件
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

示例:

person.hpp中的代码

#pragma once
#include<iostream>
#include<string>
using namespace std;

template<class T1, class T2>
class Person
{
   
public:
	Person(T1 name, T2 age);

	void showPerson();

public:
	T1 m_Name;
	T2 m_Age;
};

//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
   
	this->m_Name = name;
	this->m_Age = age;
}

//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
   
	cout << "姓名:" << this->m_Name << endl;
	cout << "年龄:" << this->m_Age << endl;
}

类模板分文件编写.cpp中代码

#include<iostream>
#include<string>
using namespace std;
//第一种解决方式	直接包含 源文件
//#include "person.cpp"

//第二种解决方式,将.h和.cpp中的内容写在一起,将后缀名改为.hpp
#include "person.hpp"

//类模板分文件编写以及解决问题
void test01()
{
   
	Person<string, int> p("张三", 18);
	p.showPerson();
}

int main()
{
   
	test01();

	return 0;
}

总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名gai’w

1.3.8 类模板与友元

学习目标: 掌握类模板配合友元函数的类内和类外实现

全局函数类内实现 —— 直接在类内声明友元即可

全局函数类外实现 —— 需要提前让编译器知道全局函数的存在

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

//通过全局函数	打印Person信息

//提前让编译器知道Person类的存在
template<class T1, class T2>
class Person;

//类外实现
template<class T1, class T2>
void printPerson2(Person<T1, T2> p)
{
   
	cout << "类外实现——姓名:" << p.m_Name << endl;
	cout << "类外实现——年龄:" << p.m_Age << endl;
}

template<class T1, class T2>
class Person
{
   
	//全局函数	类内实现
	friend void printPerson(Person<T1, T2> p)
	{
   
		cout << "姓名:" << p.m_Name << endl;
		cout << "年龄:" << p.m_Age << endl;
	}

	//全局函数	类外实现
	//加空模板参数列表
	//如果全局函数是类外实现,需要让编译器提前知道这个函数的存在
	friend void printPerson2<>(Person<T1, T2> p);

public:
	Person(T1 name, T2 age)
	{
   
		this->m_Name = name;
		this->m_Age = age;
	}

private:
	T1 m_Name;
	T2 m_Age;
};


//1. 全局函数在类内实现
void test01()
{
   
	Person<string, int> p("张三", 20);
	printPerson(p);
}

//1. 全局函数在类外实现
void test02()
{
   
	Person<string, int> p("李四", 30);
	printPerson2(p);
}

int main()
{
   
	test01();
	test02();

	return 0;
}

image-20211101203724622

总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别

1.3.9 类模板案例

案例描述: 实现一个通用的数组类,要求如下:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中的当前元素个数和数组的容量

示例:

myArraay.hpp中代码:

//自己的通用数组类
#pragma once
#include<iostream>
#include<string>
using namespace std;

template<class T>
class MyArray
{
   
public:
	//有参构造
	MyArray(int capacity)
	{
   
		//cout << "MyArray的有参构造调用" << endl;

		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}

	//拷贝构造
	MyArray(const MyArray& arr)
	{
   
		//cout << "MyArray的拷贝构造调用" << endl;

		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//浅拷贝
		//this->pAddress = arr.pAddress;

		//深拷贝
		this->pAddress = new T[this->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 << "MyArray的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[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
   
			this->pAddress[i] = arr.pAddress[i];
		}

		return *this;
	}

	//尾插法
	void Push_Back(const T& val)
	{
   
		//判断容量是否已满
		if (this->m_Capacity == this->m_Size)
		{
   
			cout << "数组容量已满,无法插入!" << endl;
			return;
		}

		this->pAddress[this->m_Size] = val;//在数组末尾插入数据
		this->m_Size++;//更新数组大小
	}

	//尾删法
	void Pop_Back()
	{
   
		//让用户访问不到最后一个元素,即为尾删
		if (this->m_Size == 0)
		{
   
			cout << "数组为空!" << endl;
			return;
		}
		this->m_Size--;
	}

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

	//返回数组容量
	int getCapacity()
	{
   
		return this->m_Capacity;
	}

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

	//析构函数
	~MyArray()
	{
   
		//cout << "MyArray的析构构造调用" << endl;

		if (this->pAddress != NULL)
		{
   
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	T* pAddress;//指针指向堆区开辟的真实的数组

	int m_Capacity;//数组容量

	int m_Size;//数组大小

};

类模板案例——数组类封装.cpp中进行测试

#include "myArray.hpp"

void printArray(MyArray<int>& arr)
{
   
	for (int i = 0; i < arr.getSize(); i++)
	{
   
		cout << arr[i] << "\t";
	}
	cout << endl;
}

void test01()
{
   
	cout << "测试一:内置数据测试" << endl;
	MyArray<int> arr1(5);

	for (int i = 0; i < 5; i++)
	{
   
		//利用尾插法向数组中插入数据
		arr1.Push_Back(i);
	}

	cout << "arr1的打印输出为:" << endl;
	printArray(arr1);

	cout << "arr1的容量为:" << arr1.getCapacity() << endl;
	cout << "arr1的大小为:" << arr1.getSize() << endl;

	MyArray<int> arr2(arr1);
	cout << "arr2的打印输出为:" << endl;
	printArray(arr2);
	//尾删
	arr2.Pop_Back();
	cout << "arr2尾删后:" << endl;
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;
}

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

//打印数组
void printPersonArray(MyArray<Person>& arr)
{
   
	for (int i = 0; i < arr.getSize(); i++)
	{
   
		cout << "姓名:" << arr[i].m_Name << "\t年龄" << arr[i].m_Age << endl;
	}
}

void test02()
{
   
	cout << "******************" << endl;
	cout << "测试二:自定义数据测试" << endl;

	MyArray<Person> arr(10);

	Person p1("一", 11);
	Person p2("二", 22);
	Person p3("三", 33);
	Person p4("四", 44);
	Person p5("五", 55);

	//将数据插入到数组中
	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.getCapacity() << endl;
	cout << "arr的大小为:" << arr.getSize() << endl;

}

int main()
{
   
	test01();
	test02();
	return 0;
}

image-20211101220227513

2. STL初始

2.1 STL的诞生
  • 长久以来,软件界一直希望建立一种可重复利用的东西
  • C++的面向对象泛型编程思想,目的就是复用性的提升
  • 大多数情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作
  • 为了建立数据结构和算法的一套标准,诞生了STL

2.2 STL基本概念

  • STL( Standard Template Library,标准模板库 )
  • STL从广义上分为:容器(container) 算法(algorithm) 迭代器(iterator)
  • 容器算法之间通过迭代器进行无缝连接
  • STL 几乎所有的代码都采用了模板类或者模板函数

2.3 STL六大组件

STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器

  1. 容器: 各种数据结构,如vector、list、deque、set、map等,用来存放数据
  2. 算法: 各种常用的算法、如sort、find、copy、for_each等
  3. 迭代器: 扮演了容器与算法之间的胶合剂
  4. 仿函数: 行为类似函数,可以作为算法的某种策略
  5. 适配器: 一种用来修饰容器或者仿函数或迭代器接口的东西
  6. 空间配置器: 负责空间的配置与管理

2.4 STL中容器、算法、迭代器

容器: 置物之所也

STL容器就是将运用最广泛的一些数据结构实现出来

常用的数据结构:数组、链表、树、栈、队列、集合、映射表等

这些容器分为序列式容器关联式容器两种:

  • 序列式容器: 强调值的排序,序列式容器的每个元素均有固定的位置
  • 关联式容器: 二叉树结构,各元素之间没有严格的物理上的顺序关系

算法: 问题之解法也

有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)

算法分为:质变算法非质变算法两种:

  • 质变算法: 是指运算过程中会更改区间内的元素的内容。例如拷贝、替换、删除等等
  • 非质变算法: 是指运算过程中不会更改区间的内容,例如查找、计数、遍历、寻找极值等等

迭代器: 容器和算法之间的粘合剂

提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表达方式。

每个容器都有自己专属的迭代器

迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针

迭代器种类:

种类 功能 支持运算
输入迭代器 对数据的只读访问 只读,支持++、==、!=
输出迭代器 对数据的只写访问 只写、支持++
前向迭代器 读写操作,并能向前推进迭代器 读写,支持++、==、!=
双向迭代器 读写操作,并能向前和向后操作 读写,支持++、–
随机访问迭代器 读写操作、可以以跳跃的方式随机访问数任意数据、功能最强的迭代器 读写,支持++、–、[n]、-n、<、<=、>、>=

常用的容器中迭代器种类为双向迭代器和随机访问迭代器

2.5 容器、算法和迭代器初始

了解STL中容器、算法、迭代器之后,我们利用代码感受STL的魅力

STL中最常用的容器为Vector,可以理解为数组,下面我们学习如何向这个容器中插入数据,并遍历这个容器。

2.5.1 vector存放内置数据类型

容器:vector

算法:for_each

迭代器:vector<int>::iterator

示例:

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

//vector容器存放内置数据类型

void myPrint(int val)
{
   
	cout << val << "\t";
}

void test01()
{
   
	//创建一个vector容器,数组
	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 << "\t";
		itBegin++;
	}
	cout << endl;

	//第二种遍历方式
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
   
		cout << *it << "\t";
	}
	cout << endl;

	//第三种遍历方式 利用STL提供的遍历算法
	for_each(v.begin(), v.end(), myPrint);

}
int main()
{
   
	test01();

	return 0;
}

image-20211105103239261

2.5.2 Vector存放自定义数据类型

学习目标: vector中存放自定义数据类型,并打印输出

示例:

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

//vector容器中存放自定义数据类型

class Person
{
   
public:
	Person(string name, int age)
	{
   
		m_Name = name;
		m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
};

void test01()
{
   
	vector<Person> v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值