C++模板和STL
- 主要针对c++ 泛型编程和STL技术做详细讲解,探讨c++更深层的使用
一、模板
1.概念
特点:
- 模板不可以直接使用,只是一个框架
- 模板的通用并不是万能的
2.函数模板
- c++的一种编程思想称为泛型编程,主要利用的技术就是模板
- c++提供两种模板机制函数模板和类模板
2.1 函数模板语法
函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体定制,用一个虚拟的类型来代表。
语法:
template<typename T>
函数声明或定义
解释:
- template:声明创建模板
- typename:其后面的符号是一种数据类型,可以用class代替
- T:通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
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;
}
void test01(){
int a = 10;
int b = 20;
swapInt(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
double c = 1.1;
double d = 2.2;
swapDouble(c, d);
cout << "c=" << c << endl;
cout << "d=" << d << endl;
}
int main(){
test01();
system("pause");
return 0;
}
利用函数模板可简化为:
#include<iostream>
using namespace std;
//函数模板
template<typename T> //声明一个模板告诉编译器后面代码中紧跟着T不要报错,T是一个通用的数据类型
void mySwap(T &a, T &b){
T temp = a;
a = b;
b = temp;
}
void test01(){
int a = 10;
int b = 20;
//swapInt(a, b);
//利用函数模板交换
//两种方式使用函数模板
//1.自动类型传导
mySwap(a, b);
//2.显示指定类型
mySwap<int>(a,b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
int main(){
test01();
system("pause");
return 0;
}
2.2 函数模板注意事项
- 自动类型推导,必须推导出一致的数据类型T,才可以使用(见2.1中交换的例子)
- 模板必须要确定出T的数据类型,才可以使用
template<class T>
void func(){
cout << "func 调用" << endl;
}
func(); //错误
func<int>(); //正确输出
2.3 函数模板案例
案例描述:
- 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
- 排序规则从大到小,排序算法为选择排序
- 分别利用char数组和int数组进行测试
#include<iostream>
using namespace std;
//实现通用 对数组进行排序的函数
//规则 从大到小
//算法 选择
//测试 char数组、int数组
//交换函数模板
template<class T>
void mySwap(T &a, T &b){
int temp = a;
a = b;
b = temp;
}
//排序算法
template<class 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<class T>
void printArray(T arr[], int len){
for(int i=0; i<len ;i++){
cout << arr[i] << " ";
}
cout << endl;
}
void test01(){
//测试cahr数组
char charArr[] = "badcfe";
int num = sizeof(charArr) / sizeof(charArr[0]);
mySort(charArr, num);
printArray(charArr, num);
}
void test02(){
//测试int数组
int intArr[] = {7,5,6,1,8,12,324,12,45};
int num = sizeof(intArr) / sizeof(int);
mySort(intArr, num);
printArray(intArr, num);
}
int main(){
test01();
test02();
system("pause");
return 0;
}
2.4 普通函数与函数模板的区别
- 普通函数调用时可以发生自动类型转换(隐式类型转换)
- 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
- 如果利用显示指定类型的方式,可以发生隐式类型转换
#include<iostream>
using namespace std;
//普通函数与函数模板的区别
//1.普通函数调用可以发生隐式类型转换
//2.函数模板 用自动类型推导,不可以发生隐式类型转换
//3.函数模板 用显示指定类型,可以发生隐式类型转换
//普通函数
int myAdd01(int a, int b){
return a + b;
}
//函数模板
template<class T>
myAdd02(T a, T b){
return a + b;
}
void test01(){
int a = 10;
int b = 20;
char c = 'c'; //a对应97, c对应99
cout << myAdd01(a, c) << endl;
//自动类型推导 不会发生隐式类型转换
//cout << myAdd02(a, c) << endl;
//显式指定类型
cout << myAdd02<int>(a, c) << endl;
}
int main(){
test01();
system("pause");
return 0;
}
2.5 普通函数与函数模板的调用规则
- 如果函数模板和普通函数都能实现,优先调用普通函数
- 可以通过空模板参数列表来 强制调用 函数模板
- 函数模板也可以发生重载
- 如果函数模板可以产生更好的匹配,优先调用函数模板
#include<iostream>
using namespace std;
void myPrint(int a, int b){
cout << "调用普通函数" << endl;
}
template<class T>
void myPrint(T a, T b){
cout << "调用的模板" << endl;
}
template<class T>
void myPrint(T a, T b, T c){
cout << "调用重载的模板" << endl;
}
void test01(){
int a = 10;
int b = 20;
//myPrint(a, b);
//通过空模板参数列表,强制调用函数模板
myPrint<>(a, b); //强制调用
myPrint(a, b, 100);
// 如果函数模板可以产生更好的匹配,优先调用函数模板
char c1 = 'a'; //结果为函数模板
char c2 = 'b';
myPrint(c1, c2);
}
int main(){
test01();
system("pause");
return 0;
}
2.6 模板的局限性
- 模板的通用性不是万能的
例:比大小的模板,当传入的参数为自定义结构体就无法比较。 需要具体化。
3.类模板
3.1 类模板语法
类模板作用:
- 建立一个通用类,类中的成员,数据类型可以不制定,用一个虚拟的类型来代表。
语法:
template<typename 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->mName = name;
this->mAge = age;
}
void showPerson()
{
cout << "name: " << this->mName << " age: " << this->mAge << endl;
}
public:
NameType mName;
AgeType mAge;
};
void test01()
{
// 指定NameType 为string类型,AgeType 为 int类型
Person<string, int> P1("孙悟空", 999);
P1.showPerson();
//name: 孙悟空 age: 999
}
int main(){
test01();
system("pause");
return 0;
}
二、STL初识
1.STL 诞生
- 长久以来,软件界一直希望建议一种可重复利用的东西
- C++的面向对象和泛型编程思想,目的就是复用性的提升
- 大多数情况下,数据结构和算法都未能有一套标准,导致被迫从事大量工作
- 为了建立数据结构和算法的一套标准,诞生了STL
2.STL基本概念
- STL(Standard Template Library,标准模板库)
- STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator)
- 容器和算法之间通过迭代器进行无缝连接
- STL几乎所有的代码都采用了模板类或者模板函数
3.STL六大组件
STL大体分为六大组件,分别为:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
- 容器:各种数据结构,如vector、list、deque、map等,用来存放数据。
- 算法:各种常用的算法,如sort、find、copy、for_each等。
- 迭代器:扮演了容器与算法之间的胶合剂。
- 仿函数:行为类似函数,可作为算法的某种策略。
- 适配器:一种用来修饰容器或者仿函数或者迭代器接口的东西。
- 空间配置器:负责空间的配置与管理。
4.STL中容器、算法、迭代器
4.1 容器
容器: 置物之所也
STL容器就是将运用最广泛的一些数据结构实现出来
常用的数据结构:数组、链表、树、栈、队列、集合、映射表 等
这些容器分为序列式容器和关联式容器两种:
序列式容器: 强调值的排序,序列式容器中每个元素都有固定位置。
关联式容器: 二叉树结构,各元素之间没有严格的物理上的顺序关系。
4.2 算法
算法: 问题之解法也
算法分为:质变算法和非质变算法
质变算法: 是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等。
非质变算法: 是指运算过程中不会更改区间内的元素内容,例如查找,计数,遍历,寻找极值等等。
4.3 迭代器
迭代器: 容器和算法之间粘合剂
提供一种方法,使之能够依序巡防某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。
每个容器都有自己专属的迭代器。
迭代器使用非常类似于指针。
迭代器种类:
种类 | 功能 | 支持运算 |
---|---|---|
输入迭代器 | 对数据的只读访问 | 只读,支持++、==、!= |
输出迭代器 | 对数据的只写访问 | 只写,支持++ |
前向迭代器 | 读写操作,并能向前和向后操作 | 读写,支持++、==、!= |
双向迭代器 | 读写操作,并能向前和向后操作 | 读写,支持++、– |
随机访问迭代器 | 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 | 读写,支持++、–、[n]、-n、<、<=、>、>= |
常用的容器中迭代器种类为双向迭代器和随机访问迭代器
5.容器算法迭代器初识
5.1 vector存放内置数据类型
容器:vector
算法:for_each
迭代器:vector<int>::iterator
#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//vector容器存放内置数据类型
void myPrint(int val){
cout << val << endl;
}
void test01(){
//创建了一个vector容器,数组
vector<int> v;
//向容器中擦汗如数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
//通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin(); //起始迭代器 指向容器中第一个元素
vector<int>::iterator itEnd = v.end(); //结束迭代器 指向容器中最后一个数
//第一种遍历方式
while(itBegin != itEnd){
cout << *itBegin << endl;
itBegin++;
}
//第二种遍历方式
for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout << *it << endl;
}
//第三种遍历方式 利用STL提供的遍历算法
for_each(v.begin(), v.end(), myPrint);
}
int main(){
test01();
system("pause");
return 0;
}
5.2 vector存放自定义数据类型
#include<iostream>
#include<vector>
using namespace std;
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;
}
5.3 vector容器嵌套容器
#include<iostream>
#include<vector>
using namespace std;
//容器嵌套容器
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);
}
//将容器元素插入到vector v中
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++) {
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << *vit << " ";
}
cout << endl;
}
}
int main() {
test01();
/*
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
*/
system("pause");
return 0;
}