STL概论
来源:互联网 发布:tensorflow 版本查看 编辑:程序博客网 时间:2024/05/20 18:16
1、容器(Containers):各种数据结构,如Vector,List,Deque,Set,Map,用来存放数据,STL容器是一种Class Template,就体积而言,这一部分很像冰山载海面的比率。
2、算法(Algorithms):各种常用算法如Sort,Search,Copy,Erase,从实现的角度来看,STL算法是一种Function Templates。
3、迭代器(Iterators):扮演容器与算法之间的胶合剂,是所谓的“泛型指针”,共有五种类型,以及其它衍生变化,从实现的角度来看,迭代器是一种将:Operators*,Operator->,Operator++,Operator--等相关操作予以重载的Class Template。所有STL容器都附带有自己专属的迭代器——是的,只有容器设计者才知道如何遍历自己的元素,原生指针(Native pointer)也是一种迭代器。
4、仿函数(Functors): 行为类似函数,可作为算法的某种策略(Policy),从实现的角度来看,仿函数是一种重载了Operator()的Class 或 Class Template。一般函数指针可视为狭义的仿函数。
5、配接器(适配器)(Adapters):一种用来修饰容器(Containers)或仿函数(Functors)或迭代器(Iterators)接口的东西,例如:STL提供的Queue和Stack,虽然看似容器,其实只能算是一种容器配接器,因为 它们的底部完全借助Deque,所有操作有底层的Deque供应。改变Functor接口者,称为Function Adapter;改变Container接口者,称为Container Adapter;改变Iterator接口者,称为Iterator Adapter。配接器的实现技术很难一言蔽之,必须逐一分析。
6、分配器(Allocators):负责空间配置与管理,从实现的角度来看,配置器是一个实现了动态空间配置、空间管理、空间释放的Class Template。
这六大组件的交互关系:container(容器) 通过 allocator(配置器) 取得数据储存空间,algorithm(算法)通过iterator(迭代器)存取 container(容器) 内容,functor(仿函数) 可以协助 algorithm(算法) 完成不同的策略变化,adapter(配接器) 可以修饰或套接 functor(仿函数)。
1.9 可能令你困惑的C++语法
1.9.4 increament/decrement/dereference 操作符实现
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class INT
{
public:
friend ostream operator<<(ostream &os, const INT &i);
public:
INT(int i): m_i(i) {};
//++i
INT &operator++()
{
++(this->m_i);
return *this;
}
//i++;
INT &operator++(int)
{
INT &tmp = *this;
++(*this);
return tmp;
}
//--i
INT &operator--()
{
--(this->m_i);
return *this;
}
//i--
INT &operator--(int)
{
INT &tmp = *this;
--(*this);
return tmp;
}
//*
int &operator*()
{
return (int &)m_i;
}
public:
int m_i;
};
ostream operator<<(ostream &os, const INT &i)
{
os << '[' << i.m_i << ']' << endl;
return os;
}
195 前毕后开区间表示法[)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T &value)
{
while(first != last && *first != value)
{
++first;
}
return first;
}
//顺序遍历算法
template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for(; first != last; ++first)
f(*first);
return f;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//C 语言的qsort 的用法
int fcmp(const void *elem1, const void *elem2)
{
const int *i1 = (const int *)elem1;
const int *i2 = (const int *)elem2;
if(*i1 < *i2)
{
return -1;
}
else if(*i1 > *i2)
{
return 1;
}
else
{
return 0;
}
}
STL 传递参数可以通过仿函数实现,就是使用起来像函数一样的东西。 例如对operator()重载。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct plus
{
T operator()(const T &x, const T &y) const
{
return (x + y);
}
};
template<class T>
struct minus
{
T operator()(const T &x, const T &y) const
{
return (x - y);
}
};
- STL-概论
- STL概论
- STL概论
- 【STL】stl概论
- STL源码剖析之一:概论
- STL源码剖析——STL概论
- STL源码:关联容器概论 树的概论
- 1 STL概论与版本简介
- 一 STL概论与版本简介
- STL源代码剖析笔记----第一章STL概论与版本简介
- STL源码剖析一——STL概论
- 概论
- 概论
- 概论
- 第1章 STL概论与版本简介
- STL源码剖析_读书笔记:第一章 STL概论和版本简介
- 《STL源码剖析》学习笔记系列-----第一章:STL概论和版本简介
- 【STL源码剖析读书笔记】【第1章】STL概论与版本简介
- 第三个是的心发放
- SCA点点滴滴(一)
- 简单的设置解决jsp页面向mysql数据库传中文乱码问题
- poj_2823(单调队列)
- HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和)
- STL概论
- POJ 1651 Multiplication Puzzle (区间dp 矩阵连乘)
- HDU 1568 Fibonacci(斐波那契前四位)
- 如何快速构建基于Spring4.0的Rest API
- hiho一下 第四周 Hihocoder #1036 : Trie图
- HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂)
- Cocos2d-x中Lua脚本的加密
- Ubuntu中ckhconfig不能用了,解决办法
- HDU 2254 奥运(矩阵快速幂+二分等比序列求和)