C++ STL与泛型编程-第一篇 (Boolan)
2017-03-02 本文已影响0人
Haley_2013
C++ STL与泛型编程-第一篇 (Boolan)
本章内容:
1 C++ Standard Library vs. Standard Template Library
2 C++的几个重要网页
3 STL相关基础知识
3.1 STL六大部件
3.2 STL六大部件关系
3.3 容器-结构与分类
4 容器使用示例
5 分配器使用示例
1 C++ Standard Library vs. Standard Template Library
- C++ Standard Library -- C++标准库
- Standard Template Library -- STL,标准模板库
标准库以header files
形式呈现: - C++标准库的
header files
不带后缀名(.h),例如#include <vector>
- 新式
C header files
不带后缀名.h,例如#include <cstdio>
- 旧式
C header files
(带后缀名.h)仍然可用,例如#include <stdio.h>
- 新式
headers
内的组件封装于namespace std
,用法如下:
1).using namespace std;
2).using std::cout;
- 旧式
headers
内的组件不封装于namespace std
头文件使用示例:
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
2 C++的几个重要网页
1). www.cplusplus.com
2). en.cppreference.com
3). gcc.gnu.org
3 STL相关基础知识
3.1 STL六大部件
- 容器(Containers)
- 分配器(Allocators)
- 算法(Algorithms)
- 迭代器(Interators)
- 适配器(Adapters)
- 仿函数(Functors)
3.2 STL六大部件关系
-
六大部件之间的关系如下图所示:
六大部件关系图
-
六大部件代码调用示例:
代码调用示例
3.3 容器-结构与分类
-
STL中容器结构分成如下几种:
结构分类
4 容器使用示例
-
公共函数部分如下所示:
公共函数
4.1 使用容器array

-
array容器的相关测试代码和结果如下:
array测试代码和结果
4.2 使用容器vector

-
vector容器的相关测试代码和结果如下:
vector测试代码
vector测试代码和结果
4.3 使用容器list

-
list容器的相关测试代码和结果如下:
list测试代码和结果
4.4 使用容器forward_list

-
forward_list容器的相关测试代码和结果如下:
forward_list测试代码和结果
4.5 使用容器deque

-
容器deque的内存结构如下图所示:
deque内存结构
-
deque容器的相关测试代码和结果如下:
deque测试代码和结果
4.6 使用容器stack

-
stack容器的相关测试代码和结果如下:
stack测试代码和结果
4.7 使用容器queue

-
queue容器的相关测试代码和结果如下:
queue测试代码和结果
4.8 使用容器multiset

-
multiset容器的相关测试代码和结果如下:
multiset测试代码和结果
4.9 使用容器multimap

-
multimap容器的相关测试代码和结果如下:
multimap测试代码和结果
4.10 使用容器unordered_multiset

- unordered_multiset容器的相关测试代码和结果如下:
unordered_multiset测试代码
![unordered_multiset测试代码]和结果](https://img.haomeiwen.com/i4614591/ecd870b07f3f600f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.11 使用容器unordered_multimap

-
unordered_multimap容器的相关测试代码和结果如下:
unordered_multimap测试代码和结果
4.12 使用容器set

-
set容器的相关测试代码和结果如下:
set测试代码和结果
4.13 使用容器map

-
map容器的相关测试代码和结果如下:
map测试代码和结果
5 分配器使用示例
-
分配器的使用如下图所示:
allocator
-
STL分配器allocator有如下几种:
array_allocator
mt_allocator
debug_allocator
pool_allocator
bitmap_allocator
malloc_allocator
new_allocator
-
分配器的结构和使用示例如下图所示:
分配器的结构和使用
-
分配器的测试代码如下图所示:
分配器的测试代码