侯捷STL学习(11)--算仿+仿函数+适配器
layout: post
title: 侯捷STL学习(十一)
date: 2017-07-24
tag: 侯捷STL
第三讲 标准库内核分析-算法
- 标准库算法形式

iterator分类
- 不同容器iterator类型不同

- 测试iterator类型
#include <iostream> // std::cout
#include <iterator> // std::iterator_traits
#include <typeinfo> // typeid
namespace jj33
{
void _display_category(random_access_iterator_tag)
{ cout << "random_access_iterator" << endl; }
void _display_category(bidirectional_iterator_tag)
{ cout << "bidirectional_iterator" << endl; }
void _display_category(forward_iterator_tag)
{ cout << "forward_iterator" << endl; }
void _display_category(output_iterator_tag)
{ cout << "output_iterator" << endl; }
void _display_category(input_iterator_tag)
{ cout << "input_iterator" << endl; }
template<typename I>
void display_category(I itr)
{
typename iterator_traits<I>::iterator_category cagy;
_display_category(cagy);
cout << "typeid(itr).name()= " << typeid(itr).name() << endl << endl;
//The output depends on library implementation.
//The particular representation pointed by the
//returned valueis implementation-defined,
//and may or may not be different for different types.
}
void test_iterator_category()
{
cout << "\ntest_iterator_category().......... \n";
display_category(array<int,10>::iterator());
display_category(vector<int>::iterator());
display_category(list<int>::iterator());
display_category(forward_list<int>::iterator());
display_category(deque<int>::iterator());
display_category(set<int>::iterator());
display_category(map<int,int>::iterator());
display_category(multiset<int>::iterator());
display_category(multimap<int,int>::iterator());
display_category(unordered_set<int>::iterator());
display_category(unordered_map<int,int>::iterator());
display_category(unordered_multiset<int>::iterator());
display_category(unordered_multimap<int,int>::iterator());
display_category(istream_iterator<int>());
display_category(ostream_iterator<int>(cout,""));
}
}
- istream_iterator,ostream_iterator的iterator_category

iterator_category对算法效率的影响
distance函数实现
对于连续空间直接相减,不连续空间需要重新计算

type traits对算法的影响
traits为萃取机,将iterator和type放进去,然后回答一些属性问题?

输入迭代器可读,输出迭代器可写。
算法模板参数输入名称,有时候暗示该算法输入的迭代器类型
算法源码剖析例子
- sort算法,区分C函数和STL库函数

- 本身遍历容器iterator就是有序的

- 算法accumulate

- 算法for_each

- 算法replace,replace_if,replace_copy

- 算法count,count_if
- 全局函数和成员函数的区别

- 算法find,find_if

- reverse iterator:rbegin(),rend()

- 算法binary_search
- 有序查找,红黑树是怎么查找的?

仿函数functions
- 仿函数只为算法服务

- GNU C++独有的,非标准;identity在set容器中取data

- 没有继承就没有融入STL体系

- 仿函数可适配的条件:继承
- STL规定每个adaptable function都挑选适当着继承
- 仿函数是个函数对象,用小括号创建临时对象,实现的是函数功能,但是封装为class;方便adater去修饰调整

适配器Adapter
- 改造器

- A用B的方法,两种方式:继承B或者内涵B
- adapter内涵仿函数,迭代器,容器实现
- 容器适配器
- stack,queue严格的是adapter;改造就是将一些函数重新封装,换名称

- 函数适配器:binder2nd
- 要问函数内部的参数类型及其返回值类型,就需要函数本身继承的calss回答,Operation::second_agrument 就由less内部继承的父类回答

- 已经更新用bind替换binder2nd;
- 函数适配器not1

新型适配器bind
bind应用

迭代器适配器
reverse_iterator适配器

inserter适配器
不必担心目的地址是否有足够空间
copy函数已经写好了,通过操作符重载实现安全的赋值操作

ostream_iterator

istream_iterator
实际当创建istream_iterator<>对象时已经再读入都一个数据了,这个时候就要求cin输入操作

侯捷STL学习(11)--算仿+仿函数+适配器的更多相关文章
- 侯捷STL学习(12)--STL相关内容hash+tuple
layout: post title: 侯捷STL学习(12) date: 2017-08-01 tag: 侯捷STL --- 第四讲 STL相关的内容 Hash Function 将hash函数封装 ...
- 侯捷STL学习(十)--容器hashtable探索(unordered set/map)
layout: post title: 侯捷STL学习(十) date: 2017-07-23 tag: 侯捷STL --- 第二十三节 容器hashtable探索 hashtable冲突(碰撞)处理 ...
- 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)
layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...
- 侯捷STL学习(八)-- 深度探索deque
layout: post title: 侯捷STL学习(八) date: 2017-07-19 tag: 侯捷STL --- 第十八节 深度探索deque上 duque内存结构 分段连续,用户看起来是 ...
- 侯捷STL学习(七)--深度探索vector&&array
layout: post title: 侯捷STL学习(七) date: 2017-06-13 tag: 侯捷STL --- 第十六节 深度探索vector vector源码剖析 vector内存2倍 ...
- 侯捷STL学习(一)
开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...
- 侯捷STL学习(一)--顺序容器测试
开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...
- 侯捷STL学习(四)--allocator和容器时间的实现关系
第十一节 分配器 分配器的好坏影响到容器的性能 operator new()里面调用malloc D:\Program Files (x86)\Microsoft Visual Studio 12.0 ...
- 侯捷STL学习(五)--allocator和容器之间的实现关系
第十一节 分配器 STL源码学习----内存管理 分配器的好坏影响到容器的性能 operator new()里面调用malloc D:\Program Files (x86)\Microsoft Vi ...
随机推荐
- MySQL锁机制和PHP锁机制
模拟准备--如何模拟高并发访问一个脚本:apache安装文件的bin/ab.exe可以模拟并发量 -c 模拟多少并发量 -n 一共请求多少次 http://请求的脚本例如:cmd: apache安装路 ...
- spark学习2-1(hive1.2安装)
由于前面安装版本过老,导致学习过程中出现了很多问题,今天安装了一个新一点的版本.安装结束启动时遇到一点问题,记录在这里. 第一步:hive-1.2安装 通过WinSCP将apache-hive-1.2 ...
- Codeforces Beta Round #61 (Div. 2) D. Petya and His Friends 想法
D. Petya and His Friends time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Windows下软件调试
1. 视频: (1).VS下的C++调试方法.wnv (2).WinDbg高级调试技术.wmv (3).内存与句柄泄漏处理技巧.wmv 2. “WinDbg高级调试技巧” 中 [01:22]讲到“软件 ...
- css开发素材网址
1.border-collapse 为表格设置合并边框模型 2.border-spacing border-spacing 属性设置相邻单元格的边框间的距离 backface-visibility:h ...
- C语言基础一
C语言学习 C语言的特点 语言简单.紧凑.使用方便.灵活 运算符丰富 数据类型丰富 具有结构化的控制语句 语法限制不太严格,程序设计自由度大 C语言允许直接访问物理地址,可以对硬件进行直接操作 生成代 ...
- 51nod 1272 思维/线段树
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...
- php 5.6以上可以采用new PDD连接数据库的方法。
<?php// @mysqli_connect($db=localhost,// $_cont['root'],// $_cont['root'],// $_cont['demo'],// $_ ...
- 第三方开源--Android Image Cropper--图片裁剪
github下载地址:https://github.com/ArthurHub/Android-Image-Cropper 有两种使用方式: 第一种:Activity用法 1.添加 CropImage ...
- Mybatis_总结_06_用_插件开发
一.前言 Mybatis采用责任链模式,通过动态代理组织多个插件(拦截器),通过这些插件可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的 ...