c++中STL

STL(starand template lib):标准模板库

背景:
1、c++面向对象和泛型编程思想,目的是复用性的提升
2、为了建立数据结构和算法的一套标准诞生了STL

  • STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator)
  • 容器和算法之间通过迭代器无缝对接
  • STL几乎所有的代码都采用了模板类和模板函数

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

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

容器
容器可分为序列式容器和关联式容器

函数对象(仿函数)
概念

  • 重载函数调用操作符的类,其对象称为函数对象
  • 函数对象使用重载的()时,其行为类似函数调用,也叫仿函数

本质:
函数对象(仿函数)本质上是一个类,不是一个函数

函数对象使用:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,也可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递

下面列子:

1//函数对象在使用时,可以像普通函数那样调用,可以有参数,也可以有返回值
 class MyAdd {
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd myAdd;
	cout << myAdd(10, 10) << endl; //这里对象使用就像函数调用
}


2、函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyAdd {
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
		count++;
	}
    int count; 记录operator()函数调用的次数 也就是有自己的状态
};

3、函数对象可以作为参数传递
void doPrint(MyAdd &myAdd, int v1, int v2)
{
    myAdd(v1, v2);
}

谓词
概念:

  • 返回bool类型的仿函数称为谓词
  • 如果operator()接收一个参数则称为一元谓词
  • 如果operator()接收两个参数则称为二元谓词

其中一元谓词的例子如下:

class PrintWorker {
public:
	bool operator()(int val)  //一元谓词的返回必须时bool
	{
		return val > 5;
	}
};

void test03()
{
	vector<int> v;
	for (int i = 0; i < 5; i++)
	{
		v.push_back(i);
	}

	vector<int>::iterator pos = find_if(v.begin(), v.end(), PrintWorker()); //返回满足条件的迭代器  PrintWorker()对象函数返回true时
	if (pos != v.end())
	{
		cout << "找到了大于5的数" << endl;
		cout << *pos << endl;
	}
	else {
		cout << "m没找到大于5的数" << endl;
	}
}

二元谓词例子如下:

//二元谓词
class MyCompare {
public:
	bool operator()(int val1, int val2)
	{
		return val1 > val2;
	}
};

void test04()
{
	vector<int> v;
    v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(20);
	v.push_back(60);
	v.push_back(50);
	
	//系统默认提供的sort 算法策略是默认从小到大
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}

	//使用函数对象(二元谓词)改变算法策略,改变规则为从大到小
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}
}

下面讲述内建函数对象

内建函数对象:STL定义了一些函数对象

分类:

  • 算术仿函数
  • 关系仿函数
  • 逻辑仿函数

使用内建函数对象需要引入头文件 #include

算术仿函数:(+、-、*、/、!)

#include 
void test05()
{
	//negate 一元仿函数 取反
	negate<int> n;
	cout << n(20) << endl;  //结果-20
    // plus 二元仿函数 加法
    plus<int> p;
	cout << p(10, 20) << endl;
}

关系仿函数: (大于、小于)

这里是在学习shared_ptr智能指针时在操作STL iterator迭代器出现的错误

#include 
#include 
#include 

using namespace std;
void listTest()
{
	//list里依次插入shared_ptr管理的string对象
	list<shared_ptr<string>> pstrList;
	pstrList.emplace_back(make_shared<string>("111111"));
	pstrList.emplace_back(make_shared<string>("222222"));
	pstrList.emplace_back(make_shared<string>("333333"));
	pstrList.emplace_back(make_shared<string>("444444"));

	for (auto p : pstrList)
	{
		cout << *p << endl;
	}

	//这里需要完成删除erase指定对象
	std::cout << "itertor erase test!!!" << std::endl;
	for (list<shared_ptr<string>>::iterator itr = pstrList.begin(); itr != pstrList.end(); ++itr)
	{
		if (**itr == "333333")
		{
			cout << **itr << endl;
			itr = pstrList.erase(itr); //这里需要注意erase后返回的itertor迭代器就指向了下一个元素
			--itr;         //所以这里需要-- ,不--的话就要出现cannot increment end list iterator错误
		}
	}

	//打印erase之后的list
	std::cout << "after erase!!!" << std::endl;
	for (auto p : pstrList)
	{
		cout << *p << endl;
	}
}
int main()
{	
	listTest();
}

你可能感兴趣的:(C++,音视频,wireshark,测试工具)