c++提高部分

c++提高部分

这部分主要涉及泛型编程和STL技术

1. 模版

1.1 模版的概念

模版就是通用的模具,大大提高复用性,但需要根据需求改动一些东西

1.2 函数模版

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

1.2.1 函数模板语法

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

语法:

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

解释:
template——声明创建模板

typena me——表明其后面的 符号为一种数据类型,可以用class代替。

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

#include<iostream>
using namespace std;


//函数模板
//两个整型交换
void  SwapInt(int &a, int &b)
{
	int temp = b;
	b = a;
	a = temp;
}
//两个浮点型交换
void SwapDouble(double &a,double & b)
{
	double temp = a;
	a = b;
	b = temp;
}


//函数模板
//声明一个模板,告诉编译器后面的代码中紧跟着的T不要报错,T是一个通用数据类型
template<typename T>
void MySwap(T& a, T& b)
{
	T Temp = a;
	a = b;
	b = Temp;
}

void test01()
{
	int a = 10;
	int b = 20;
	//利用函数模板进行交换
	//1.自动类型推导
	MySwap(a, b);
cout << a  << endl;
cout << b << endl;

double c = 11.1;
double d = 12.2;
//显示指定类型
MySwap<double>(c, d);
cout << c << endl;
cout << d << endl;
}
int main(void)
{
	test01();
	system("pause");
	return 0;
}

总结

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

1.2.2 函数模板注意事项:
  • 自动类型推导,必须推导出一致的数据类型T才能使用
#include<iostream>
using namespace std;
template<typename T>
void myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

void test01()
{
	int a = 10;
	int b = 20;
	char t = 'l';
	//myswap(a, l);     //不一样的数据类型使用模板会报错
	cout << a << endl;
	cout << b << endl;

}

int main()
{
	test01();
	system("pause");
	return 0;
}
  • 模板必须要确定出T的数据类型,才可以使用
#include<iostream>
using namespace std;
template<typename T>
void myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

template<typename T>
void func()
{
	cout << "hello!" << endl;
}

void test01()
{
	int a = 10;
	int b = 20;
	char t = 'l';
	cout << a << endl;
	cout << b << endl;

	//func();           //模板必须要确定出T的数据类型,才可以使用
	func<int>();       //或者这样,可以使用

}

int main()
{
	test01();
	system("pause");
	return 0;
}
1.2.3 函数模板案例

案例描述:

  • 利用函数模板封装一个排序的函数,可以对不用数据类型数组进行排序
  • 排序规则从大到小,排序算法为选择排序
  • 分别利用char数组和int数组进行测试
#include<iostream>
using namespace std;

template<class T>
void myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

template<class T>
void sort(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		int flag = i;
		for (int j = i + 1; j < len; j++)
		{
			if (arr[flag] < arr[j])
			{
				flag = j;
			}

		}
		if (flag != i)
		{
			myswap(arr[i], arr[flag]);
		}

	}
}

template<class T>
void my_print(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << '\t';
	}
	cout << endl;
}

void test01()
{
	char arr[] = "adecb";
	int num = sizeof(arr) / sizeof(char);
	sort(arr, num);
	my_print(arr, num);


	int n_arr[] = { 2,4,1,6,8,7 };
	int num1 = sizeof(n_arr) / sizeof(int);
	sort(n_arr, num1);
	my_print(n_arr, num1);

}

int main()
{
	test01();
	system("pause");
	return 0;
}
1.2.4 普通函数与函数模板的区别

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

  • 普通函数调用时可以发生自动类型转换(隐式类型推导)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显式指定类型的方式,可以发生隐式类型转换
#include<iostream>
using namespace std;
//普通函数与函数模板的区别
//普通函数调用可以发生隐式类型转换
//函数模板用自动类型推导不可以发生隐式类型转换
// 函数模板用显式指定类型 可以发生隐式类型转换

//普通函数隐式类型转换
int myAdd01(int a, int b)
{
	return a + b;
}

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


void test01()
{
	int a = 10;
	int b = 20;
	char c = 'c';
	cout << myAdd01(a, c) << endl;

	//自动类型推导不行
	//cout << myAdd02(a, c) << endl;

	//显式指定类型行
	cout << myAdd02<int>(a, c) << endl;
}


int main(void)
{
	test01();
	system("pause");
	return 0;
}
1.2.5 普通函数与函数模板的调用规则

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数
  2. 可以通过空模板参数列表来强制调用函数模板
  3. 函数模板也可以发生重载
  4. 如果函数模板可以产生更好的匹配,优先调用函数模板
#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,普通函数还需要去同时转换为int,还不如直接给模板,直接转为T类型
	char c1 = 'a';
	char c2 = 'b';

	myPrint(c1, c2);
}
int main(void)
{
	test01();
	system("pause");
	return 0;
}
1.2.6 模板的局限性
#include<iostream>
#include<string>
using namespace std;

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


template<class T>
bool compare(T& a, T& b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template <>bool compare(Person& a, Person& b)
{
	if (a.age == b.age && a.name == b.name)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void test01()
{
	Person s1("il", 20);
	Person s2("il", 22);
	bool ret = compare(s1, s2);
	if (ret)
		cout << "s1=s2" << endl;
	else
		cout << "s1!=s2" << endl;
}


int main()
{
	test01();
	system("pause");
	return 0;
}

1.3 类模板

1.3.1 类模板基本语法
#include<iostream>
#include<string>
using namespace std;

template <class NameType, class AgeType>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	NameType m_name;
	AgeType m_age;
	void show()
	{
		cout << this->m_age << endl;
		cout << this->m_name << endl;
	}
};

void test01()
{
    //Person<string, int>里的string和int是给那个Name和AgeType导向的,后面直接跟对象
	Person<string, int>p1("张三", 456);
	p1.show();
}

int main()
{
	test01();
	system("pause");
	return 0;
}
1.3.2 类模板与函数模板的不同
#include<iostream>
#include<string>
using namespace std;


template <class NameType, class AgeType = int>    //类模板可以在这里指定默认参数类型
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	NameType m_name;
	AgeType m_age;
	void show()
	{
		cout << this->m_age << endl;
		cout << this->m_name << endl;
	}
};


//void test01()
//{
//	Person<string, int>p1("张三", 456);   //1.类模板必须用这种指定类型的方式赋值
//	p1.show();
//}


void test02()
{
	Person<string>p2("李逵", 26);   //1.类模板必须用这种指定类型的方式赋值
	p2.show();
}

int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}
1.3.3 类模板成员函数创建时机
#include<iostream>
#include<string>
using namespace std;

class Person1
{
public:
	void func1()
	{
		cout << "this is Person1" << endl;
	}
};


class Person2
{
public:
	void func2()
	{
		cout << "this is Person2" << endl;
	}
};

template <class T>
class Person
{
public:
	T obj;
	void show1()
	{
		obj.func1();
	}
	void show2()
	{
		obj.func2();
	}
};


void test02()
{
	Person<Person1>p1;
	p1.show1();
}

int main()
{
	//test01();
	test02();
	system("pause");
	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;
	}
	T1 m_name;
	T2 m_age;

	void showPerson()
	{
		cout << "姓名:" << this->m_name << endl;
		cout << "年龄:" << this->m_age << endl;
	}
};


//1.指定传入类型,一般最常用
void printPerson1(Person<string, int>& p1)
{
	p1.showPerson();
}
void test01()
{
	Person<string, int>p1("孙悟空", 1520);
	printPerson1(p1);
}

//2.参数模板化
template<class T1, class T2>
void printPerson2(Person<T1, T2>& p2)
{
	p2.showPerson();
	cout << typeid(T1).name() << endl;   //查看推测的数据类型
	cout << typeid(T2).name() << endl;
}
void test02()
{
	Person<string, int>p2("杀神", 320);
	printPerson2(p2);
}

//3.整个类模板化
template<class T>
void printPerson3(T& p3)
{
	p3.showPerson();
	cout << typeid(T).name() << endl;   //查看推测的数据类型
}
void test03()
{
	Person<string, int>p3("刀郎", 6820);
	printPerson3(p3);
}


int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}
1.3.5 类模板与继承

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

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

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

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

template <class T>
class Base
{
	T obj;
};

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

};

void test01()
{
	Son s1;
}

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


int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
1.3.6 类模板成员函数类外实现

能够掌握类模板中的成员函数类外实现

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

template <class T1, class T2>
class Base
{
public:
	Base(T1 name, T2 age);
	void show();
	T1 m_name;
	T2 m_age;
};

template <class T1, class T2>
Base<T1, T2>::Base(T1 name, T2 age)
{
	this->m_name = name;
	this->m_age = age;;
}

template <class T1, class T2>
void Base<T1, T2>::show()
{
	cout << "姓名为:" << this->m_name << endl;
	cout << "年龄为:" << this->m_age << endl;
}

void test01()
{
	Base<string, int> b1("李青", 80);
	b1.show();
}


int main()
{
	test01();
	system("pause");
	return 0;
}
1.3.7 类模板分文件编写

学习目标:

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

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

1.类模板分文件编写.cpp文件中只写test01(),和main函数

#include<iostream>
#include"Base.h"

void test01()
{
	Base<string, int> b1("李青", 80);
	b1.show();
}


int main()
{
	test01();
	system("pause");
	return 0;
}

2.创建Base.h头文件

#pragma once
#include<iostream>
#include<string>
using namespace std;
template <class T1, class T2>
class Base
{
public:
	Base(T1 name, T2 age);
	void show();
	T1 m_name;
	T2 m_age;
};

3.创建Base.cpp源文件

#include"Base.h"
template <class T1, class T2>
Base<T1, T2>::Base(T1 name, T2 age)
{
	this->m_name = name;
	this->m_age = age;;
}

template <class T1, class T2>
void Base<T1, T2>::show()
{
	cout << "姓名为:" << this->m_name << endl;
	cout << "年龄为:" << this->m_age << endl;
}

此时编译没问题,但是运行会有问题。运行的时候编译器只是看到了Base.h头文件的文件内容两次,并没有看到实现。

解决

解决方式1:类模板分文件编写.cpp文件中直接包含Base.cpp源文件

#include<iostream>
#include"Base.cpp"

void test01()
{
	Base<string, int> b1("李青", 80);
	b1.show();
}


int main()
{
	test01();
	system("pause");
	return 0;
}

解决方式2:将声明.h和实现.cpp在到同一个文件中,并更改后缀名为.hpp,hpp是约定俗成的名称,并不是强制

  1. 在头文件部分新建Base.hpp源文件,其中包含Base.h和Base.cpp文件的内容
#pragma once
#include<iostream>
#include<string>
using namespace std;
template <class T1, class T2>
class Base
{
public:
	Base(T1 name, T2 age);
	void show();
	T1 m_name;
	T2 m_age;
};
template <class T1, class T2>
Base<T1, T2>::Base(T1 name, T2 age)
{
	this->m_name = name;
	this->m_age = age;;
}

template <class T1, class T2>
void Base<T1, T2>::show()
{
	cout << "姓名为:" << this->m_name << endl;
	cout << "年龄为:" << this->m_age << endl;
}
  1. 类模板分文件编写.cpp文件中直接包含Base.hpp源文件
#include<iostream>
#include"Base.hpp"
void test01()
{
	Base<string, int> b1("李青", 80);
	b1.show();
}


int main()
{
	test01();
	system("pause");
	return 0;
}
1.3.8 类模板与友元
  1. 类内实现
#include<iostream>
#include<string>
using namespace std;

template <class T1, class T2>
class Perosn
{
	//友元类内实现
	friend void show(Perosn<string, int>p)
	{
		cout << "姓名:" << p.m_name << ",年龄:" << p.m_age << endl;
	}
public:
	Perosn(T1 name, T2 age)
	{
		this->m_name = name;
		this->m_age = age;
	}
private:
	T1 m_name;
	T2 m_age;
};


void test01()
{

	Perosn<string, int>p1("张三", 45);
	show(p1);
}


int main()
{
	test01();
	system("pause");
	return 0;
}
  1. 类外实现
#include<iostream>
#include<string>
using namespace std;

//提前让编译器知道Person类的存在
template<class T1, class T2 >
class Person;
//类外实现
template<class T1, class T2>
void show_p(Person<T1, T2>p)
{
	cout << "姓名:" << p.m_name << ",年龄:" << p.m_age << endl;
}


template <class T1, class T2>
class Person
{
	//加空模板参数列表
	friend void show_p<>(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;
};


void test01()
{

	Person<string, int>p1("小期待", 154);
	show_p(p1);
}


int main()
{
	test01();
	system("pause");
	return 0;
}

在此例中,全局函数friend void show_p<>(Person<T1, T2>p);这是一个普通函数,得让编译器提前知道全局函数的存在,也需要知道Person是一个模板。

1.3.9 类模板案例

案例描述:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组汇中当前元素个数和数组的容量
  1. MyArray.hpp文件中编写
#pragma once
#include<iostream>
#include<string>
using namespace std;

template <class T>
class Myarray
{
public:
	//有参构造函数
	Myarray(int capacity)
	{
		cout << "MyArry的有参构造函数调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}
	//拷贝构造函数
	Myarray(Myarray& m1)
	{
		cout << "MyArry的拷贝构造函数调用" << endl;
		this->m_Capacity = m1.m_Capacity;
		this->m_Size = m1.m_Size;
		//深拷贝
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < m1.m_Size; i++)
		{
			this->pAddress[i] = m1.pAddress[i];
		}
	}

	//赋值符重载
	Myarray& operator =(Myarray& arr)
	{

		cout << "MyArry的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];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}

	~Myarray()
	{
		if (this->pAddress != NULL)
		{
			cout << "析构函数!" << endl;
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress;//指针指向堆区开辟的真实数组
	int m_Capacity;//数组容量
	int m_Size;//数组大小
};
  1. 类函数模板.cpp文件编写
#include<iostream>
#include"MyArray.hpp"

void test01()
{
	Myarray<int>a1(5);
	Myarray<int>a2(a1);
	Myarray<int>a3(12);
	a3 = a1;

}

int main()
{
	test01();
	system("pause");
	return 0;
}
  1. 在MyArray.hpp文件中继续编写统计大小、统计容量、[]重载(用于显示值)、尾插法、尾删法
#pragma once
#include<iostream>
#include<string>
using namespace std;

template <class T>
class Myarray
{
public:
	//有参构造函数
	Myarray(int capacity)
	{
		//cout << "MyArry的有参构造函数调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}
	//拷贝构造函数
	Myarray(Myarray& m1)
	{
		//cout << "MyArry的拷贝构造函数调用" << endl;
		this->m_Capacity = m1.m_Capacity;
		this->m_Size = m1.m_Size;
		//深拷贝
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < m1.m_Size; i++)
		{
			this->pAddress[i] = m1.pAddress[i];
		}
	}

	//赋值符重载
	Myarray& operator =(Myarray& arr)
	{

		//cout << "MyArry的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];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}

	//中括号重构,返回值
	T& operator [](int index)
	{
		return this->pAddress[index];
	}

	//尾插
	void push_add(T& val)
	{
		if (this->m_Capacity == this->m_Size)
			return;
		else
		{
			this->pAddress[this->m_Size] = val;
			this->m_Size++;
		}
	}
	//尾删
	void push_del()
	{
		if (this->m_Size == 0)
			return;
		else
		{
			this->m_Size--;
		}
	}

	//统计大小
	int get_size()
	{
		return this->m_Size;
	}
	//统计容量
	int get_capacity()
	{
		return this->m_Capacity;
	}
	//析构函数
	~Myarray()
	{
		if (this->pAddress != NULL)
		{
			//cout << "析构函数!" << endl;
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress;//指针指向堆区开辟的真实数组
	int m_Capacity;//数组容量
	int m_Size;//数组大小
};
  1. 类函数模板.cpp文件中测试普通数据
#include<iostream>
#include"MyArray.hpp"

void print_arr(Myarray<int>& a1)
{
	for (int i = 0; i < a1.get_size(); i++)
	{
		cout << a1[i] << endl;
	}
}

void test01()
{
	Myarray<int>a1(5);
	for (int i = 0; i < 5; i++)
	{
		a1.push_add(i);
	}
	cout << "插入之后的数据为:" << endl;
	print_arr(a1);
	cout << "数组的容量为:" << a1.get_capacity() << endl;
	cout << "数组的大小为:" << a1.get_size() << endl;
	a1.push_del();
	cout << "删除之后的数据为:" << endl;
	print_arr(a1);
	cout << "数组的容量为:" << a1.get_capacity() << endl;
	cout << "数组的大小为:" << a1.get_size() << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
  1. 在类函数模板.cpp文件中测试结构体数据
#include<iostream>
#include"MyArray.hpp"

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



void PrintPersonArry(Myarray<Person>& arr)
{
	for (int i = 0; i < arr.get_size(); i++)
	{
		cout << "姓名为:" << arr[i].m_name << "年龄为:" << arr[i].m_age << endl;
	}
}

void test02()
{

	Myarray<Person>arr(10);
	Person p1("伞兵1", 21);
	Person p2("伞兵2", 22);
	Person p3("伞兵3", 23);
	Person p4("伞兵4", 24);
	Person p5("伞兵5", 25);

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

	//打印数组
	PrintPersonArry(arr);
	//输出容量
	cout << "arr容量为" << arr.get_capacity() << endl;
	//输出大小
	cout << "arr大小为" << arr.get_size() << endl;

}

int main()
{
	test02();
	system("pause");
	return 0;
}

2. STL

2.1 STL的诞生

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

2.2 STL基本概念

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

2.3 STL六大组件

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

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

2.4 STL中容器、算法、迭代器的概念

容器:置物之所也

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

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

这些容器分为序列式容器和关联式容器

  • 序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置

  • 关联式容器:二叉树结构体,各元素之间没有严格上的物理上的顺序关系

算法:问题之解法也

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

算法分为:质变算法和非质变算法

  • 质变算法:是指运算过程中会更改区间内元素的内容。例如拷贝、查找、删除等等

  • 非质变算法:是指在运算过程中不会更改区间内的元素内容,例如查找、

计数、遍历、寻找极值等等

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

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

  • 37
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值