C++ STL 体系结构与内核分析(一)

 

 

目录

 

C++标准库

STL 六大部件

容器


C++标准库

STL 六大部件

C++STL主要由6个组件(Components)组成,分别是:容器(Containers)、分配器(Allocators)、算法(Algorithms)、迭代器(Iterators)、适配器(Adapters)、仿函数(Functors)。最重要的:容器和算法。

下面介绍每个部件基本概念:

容器(Containers)是什么?可以理解为已经实现了的,功能完善的数据结构的集合,不同的容器对应着不同的数据结构,使用不同的容器就是在使用不同的数据结构。

 分配器(Allocators)是C++STL中对内存进行分配和归还的工具。分配器(Allocators)表现出来的是特殊的内存模型(memory model),是一种用来把“内存需求”转化为“内存低级调用”的抽象层。最初是被用以处理各种指针(near,far,huge)的问题的抽象层(或者说是规范化的工具),现在在STL中被用以处理内存共享(shared memory)、垃圾回收(garbage collection)等。容器(Containers)对于分配器(Allocators)是有依赖的,容器(Containers)对象依赖于分配器(Allocators)进行内存的分配。

算法(Algorithms)中有很多算法的C++实现,大多以模板函数(Template Function)的形式存在,用以处理容器(Containers)中的数据。容器(Containers)中包含有对该容器(Containers)的一些基本操作,但更为复杂的处理就需要通过算法(Algorithms)中的各种算法(Algorithm)来实现。这里体现的是什么?不是OOP编程(面向对象编程),而是GP编程(模板编程)。使用GP编程的好处在于对算法的实现能够和单独的对象剥离开来,使得一套GP编程实现的算法(Algorithm)能够应用于多种容器(Containers)。

迭代器(Iterators)的用处在于辅助算法(Algorithms)对容器(Containers)中的数据(Data)进行处理。容器(Containers)中的数据往往不是只有单独的一条,而是大量数据的集合。而算法(Algorithms)在处理数据(Data)的时候,有时候需要面向容器(Containers)中的一个数据对象,有时候是多个,或者整体。对于这样子的需求,需要一个工具来帮助算法(Algorithms)来实现对容器(Containers)中数据的访问,而这个东西就是迭代器(Iterators)。

仿函数(Functors)是一些辅助算法(Algorithms)用于针对处理一些特殊数据对象的时候的一些自定义处理方式,用以增强算法(Algorithms)的处理能力。

适配器(Adapters)是处理一些兼容性所使用的工具。具体一点可以分为:容器适配器(Container Adapters)、迭代器适配器(Iterator  Adapters)、仿函数适配器(Functors Adapters)。

code:

/*
在这个简单的代码示例中能看到有vector类型的容器对象vi,并且指定了一个allocator分配器,
且指定了分配的是int类型。这里需要注意的是一般可以不指定分配器,在不指定分配器的情况下会
使用默认的分配器,一但使用了分配器就必须保证分配器的类型参数是正确的。count_if是一种算
法,该算法的作用是在满足某种条件的情况下进行统计,vi.begin()和 vi.end()是两个Iterat
or类型的变量,用以标记count_if算法要处理的数据的范围。not1和bind2nd是仿函数适配器(It
erators Adapters),适配器bind2nd的作用就是绑定第二参数,也就是指定仿函数less的比较对
象。而not1这个适配器的作用是条件取反,也就是将适配器not1的对象的条件进行取反。less就是仿
函数(Functors)了。
*/
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main()
{
	int ia[6] = {27, 210, 12, 47, 109, 83};
	vector<int,allocator<int> > vi(ia, ia + 6);

	cout << count_if(vi.begin(), vi.end(),
		not1(bind2nd(less<int>(), 40))) << endl;
	return 0;
}

容器

for循环

auto

Reference

https://blog.csdn.net/smallerxuan/article/details/80783108

https://www.bilibili.com/video/BV1db411q7B8?p=3

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神书-STL实现原理,对于强化数据结构-算法的程序员必备、必读书籍。The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值