【C++STL标准库】算法,仿函数与空间适配器

这篇具有很好参考价值的文章主要介绍了【C++STL标准库】算法,仿函数与空间适配器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

仿函数:

长得像函数,但是不是函数
实际上就是在类或结构体中重载了()运算符

  • 结构体仿函数:
struct aMax {
    int operator()(int a, int b) {
        return a > b ? a : b;
    }
};
  • 类仿函数:
class Max{
public:
    int operator()(int a, int b) {
        return a > b ? a : b;
    }
};
  • 使用:
int main() {
	//仿函数
	int a = Max()(5, 6);
	int b = aMax()(9, 10);
	std::cout << a << std::endl;
	std::cout << b << std::endl;
	return 0;
}
  • 运行截图:

【C++STL标准库】算法,仿函数与空间适配器,C++STL标准库,c++,算法,开发语言

空间适配器

基本概念这里就不浪费时间介绍了,代码中有介绍到,这里给出空间适配器的基本使用方式:

#include <iostream>
#include <string>

class Max{
public:
	int operator()(int a, int b) {
		return a > b ? a : b;
	}
};

struct aMax {
	int operator()(int a, int b) {
		return a > b ? a : b;
	}
};

int main() {
	//仿函数
	int a = Max()(5, 6);
	int b = aMax()(9, 10);
	std::cout << a << std::endl;
	std::cout << b << std::endl;
	//控制适配器
	//控制适配器的类型
	std::allocator<int> obj;
	int* p = obj.allocate(1);//这里实际上是申请了1个int的空间
	obj.construct(p, 12138);//将p地址的值修改为12138
	std::cout << *p << std::endl;
	*p = 100;//直接解引用也可以使用
	std::cout << *p << std::endl;
	int temp = 500;
	//address方法:获取指定类型,指定元素的指针,返回值类型:T*
	std::cout << typeid(obj.address(temp)).name() << std::endl;
	int *bAddr =obj.address(temp);
	std::cout << bAddr << "\t" << *bAddr << std::endl;
	//max_size方法,返回适配器的最大尺寸,返回值类型:unsigned int
	std::cout << typeid(obj.max_size()).name() << std::endl;
	std::cout << obj.max_size() << std::endl;
	//destory方法:释放对象空间
	obj.destroy(p);
	//deallocate方法:释放指定空间,指定大小
	obj.deallocate(p, 1);
	return 0;
}

STL算法:

基本概念这里就不浪费时间介绍了,代码中有介绍到:

#include <iostream>
#include <algorithm>
#include <vector>

struct Func {
	bool operator()(int a) {
		return a == 6;
	}
};

bool Fun5(int a) {
	return a == 5;
}

template <typename T>
void print(T Begin, T End);

void print1(int a);

int main() {
	std::vector<int> oec = { 0,4,2,3,6,8,7,51,2,2,2,2,2,2 };
	//find算法:这里介绍两种,第一种:find(起始迭代器,终止迭代器,要寻找的值)
	auto it = std::find(oec.begin(), oec.end(), 6);
	std::cout << typeid(it).name() << std::endl;
	if (it != oec.end()) {
		std::cout << "successful" << std::endl;
	}
	else {
		std::cout << "failed" << std::endl;
	}
	//第二种:find_if(起始迭代器,终止迭代器,仿函数/函数)
	auto it1 = std::find_if(oec.begin(), oec.end(), Func());
	if (it1 != oec.end()) {
		std::cout << "successful" << std::endl;
	}
	else {
		std::cout << "failed" << std::endl;
	}
	auto it2 = std::find_if(oec.begin(), oec.end(), Fun5);
	if (it2 != oec.end()) {
		std::cout << "successful" << std::endl;
	}
	else {
		std::cout << "failed" << std::endl;
	}
	//在父串中找字串,返回值类型:迭代器类型,指向第一次出现的位置
	std::vector<int> obj = { 2,3 };
	std::find_first_of(oec.begin(), oec.end(), obj.begin(), obj.end());
	std::cout << typeid(std::find_first_of(oec.begin(), oec.end(), obj.begin(), obj.end())).name() << std::endl;
	//search_n方法:
	std::search_n(oec.begin(), oec.end(), 4, 2);
	std::cout << typeid(std::search_n(oec.begin(), oec.end(), 4, 2)).name() << std::endl;


	//count方法:统计容器中,某个元素出现的次数,返回值类型:int
	std::count(oec.begin(), oec.end(), 2);
	std::cout << typeid(std::count(oec.begin(), oec.end(), 2)).name() << std::endl;
	std::cout << std::count(oec.begin(), oec.end(), 2) << std::endl;
	//count_id方法,与上述find与find_if方法差别相似
	std::count_if(oec.begin(), oec.end(), Func());
	std::cout << typeid(std::count_if(oec.begin(), oec.end(), Func())).name() << std::endl;
	std::cout << std::count_if(oec.begin(), oec.end(), Func()) << std::endl;

	//排序算法
	//1.排序,默认greater排序
	std::sort(oec.begin(), oec.end());
	print(oec.begin(), oec.end());
	std::cout << "*********************************************************************" << std::endl;
	//2.显示指定排序规则
	std::sort(oec.begin(), oec.end(), std::greater<int>());
	print(oec.begin(), oec.end());
	std::cout << "*********************************************************************" << std::endl;

	//从begin迭代器到end迭代器,每一个元素都执行print1函数
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;

	//堆排序
	//1.
	std::make_heap(oec.begin(), oec.end(), std::greater<int>());
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;
	//2.
	std::sort_heap(oec.begin(), oec.end(), std::greater<int>());
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;

	//replace算法:修改容器中的值
	std::replace(oec.begin(), oec.end(), 3, 12345);
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;

	fill算法:将所有元素重新赋值
	//std::fill(oec.begin(), oec.end(), 555);
	//std::for_each(oec.begin(), oec.end(), print1);
	//std::cout << std::endl;
	//std::cout << "*********************************************************************" << std::endl;

	//remove算法:删除指定元素
	std::remove(oec.begin(), oec.end(), 12345);
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;

	//reverse算法:将所有元素逆序
	std::reverse(oec.begin(), oec.end());
	std::for_each(oec.begin(), oec.end(), print1);
	std::cout << std::endl;
	std::cout << "*********************************************************************" << std::endl;

	return 0;
}

template<typename T>
void print(T Begin, T End)
{
	T i;
	for (i = Begin; i != End; i++) {
		std::cout << *i << std::endl;
	}
	std::cout << std::endl;
}

void print1(int a)
{
	std::cout << a << "\t";
}

  • 运行截图:
    【C++STL标准库】算法,仿函数与空间适配器,C++STL标准库,c++,算法,开发语言

如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!文章来源地址https://www.toymoban.com/news/detail-605627.html

到了这里,关于【C++STL标准库】算法,仿函数与空间适配器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】STL 算法 ⑪ ( 函数适配器嵌套用法 | modulus 函数对象 - 取模运算 | std::count_if 函数原型 | std::not1 函数原型 )

    在 functional 头文件 中 , 预定义了 modulus 函数对象 , 这是一个 二元函数对象 , 在该函数对象类中 , 重写了 函数调用操作符 函数 operator() , 该 预定义函数对象 代码如下 : 该函数对象 定义了 模板参数 template class _Ty = void , _Ty 泛型的默认参数是 void , 即 如果 不指定 模板参数 ,

    2024年01月17日
    浏览(36)
  • STL:双端队列&容器适配器&仿函数&优先级队列

    双端队列可以在头部和尾部进行插入删除操作 与vector相比,头插效率高,不需要搬移元素 与list相比,空间利用率高 deque逻辑上空间是连续的,物理上并不是,是由一段段小空间拼接而成的 双端队列的迭代器比较复杂 cur:指向空间中被遍历的那个元素 first:指向空间开始

    2024年02月16日
    浏览(32)
  • 从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系

    目录 1. C语言的输入和输出和流 2. C++的IO流 2.1 C++标准IO流 2.2 C++文件IO流 2.3 stringstream(字符流) 3. 空间配置器(了解) 3.1 一级空间适配器 3.2 二级空间配置器 3.3 二级空间适配器的空间申请 3.4 二级空间配置器的空间回收 3.5 对象构造与释放和与容器结合 4. STL六大组件联系

    2024年02月09日
    浏览(26)
  • 【C++】STL——容器适配器priority_queue(优先级队列)详解 及 仿函数的介绍和使用

    这篇文章我们接着上一篇的内容,再来学一个STL里的容器适配器—— priority_queue (优先级队列) 1.1 priority_queue的介绍 我们上一篇文章学了 queue (队列),那优先级队列也是在 queue 里面的: 和 queue 一样, priority_queue 也是一个容器适配器,那他和 queue 有什么区别呢?我们一

    2024年02月07日
    浏览(35)
  • C++ [STL容器适配器]

    本文已收录至《C++语言》专栏! 作者:ARMCSKGT 前面我们介绍了适配器模式中的反向迭代器,反向迭代器通过容器所支持的正向迭代器适配为具有反向迭代功能的迭代器,本节我们介绍STL中另一种适配器: 容器适配器 ! 前面我们提到过STL适配器模式,关于适配器的解释: S

    2024年02月11日
    浏览(32)
  • 【STL】顺序容器与容器适配器

    给出以下顺序容器表: 顺序容器类型 作用 vector 可变大小的数组,支持快速访问,除尾元素的插入或者删除很慢 string 与vector相似,只不过专门用来存字符 list 双向链表。只能双向顺序访问,插入删除的效率都很高 forward_list 单向链表。只能单向顺序访问,插入删除的效率都

    2024年04月14日
    浏览(22)
  • C++ STL学习之【容器适配器】

    ✨个人主页: 北 海 🎉所属专栏: C++修行之路 🎊每篇一句: 图片来源 A year from now you may wish you had started today. 明年今日,你会希望此时此刻的自己已经开始行动了。 适配器(配接器)是 STL 中的六大组件之一,扮演着轴承、转换器的角色,使得 STL 中组件的使用更为灵活,

    2023年04月22日
    浏览(47)
  • 面试之快速学习STL-迭代适配器

    参考:http://c.biancheng.net/view/7255.html 例子: 想使用反向迭代器实现逆序遍历容器,则该容器的迭代器类型必须是双向迭代器或者随机访问迭代器。 常见操作 注意这里不能用std::list,因为它是双向迭代器, +3的操作需要随机访问迭代器 。故联想到std::list排序只能使用list提供的

    2024年02月11日
    浏览(30)
  • STL: 容器适配器stack 与 queue

      目录 1.容器适配器 1.1 STL标准库中stack和queue的底层结构 1.2 deque的简单介绍(了解) 1.2.1 deque的原理介绍 1.2.2 deque的缺陷 1.2.3 为什么选择deque作为stack和queue的底层默认容器 2. stack的介绍和使用 2.1 stack的介绍  2.2 stack的使用 2.3 利用deque模拟实现stack 3.queue的介绍和使用 3.1 queue的

    2024年02月05日
    浏览(37)
  • STL stack,queue,deque以及适配器

    下面是stack库中的接口函数,有了前面的基础,我们可以根据函数名得知函数的作用 函数 说明 stack() 构造空栈 empty() 判断栈是否为空 size() 返回栈中元素个数 top 返回栈顶元素 push() 将值从栈顶压入栈内 pop() 在栈顶出栈 栈其实就是一种特殊的 vector ,因此可以使用 vector 模拟实

    2024年02月10日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包