C++標準庫:
C++標準庫以header files形式呈現:
(1)C++標準庫的header files不帶.h,如:#include <vector>
(2)新式的C header files不帶後綴.h,如:#include <cstdio>
(3)舊式的C header files帶後綴.h,如:#include <stdio.h>
新式的headers內的組件封裝在 namespace std 當中:using namsespace std;舊式的header內的組件不被封裝到namespace std中。
STL框架概述:
STL是屬於C++標準庫的一部分,STL有6個部件,6個部件之間有着很緊密的聯繫。
STL的6大部件包括:容器(Containers)、分配器(Allocators)、算法(Algorithm)、迭代器(Iterators)、適配器(Adapters)、仿函數(Functors)。
概述上述6大部件之間的關係如下:
容器用於存儲數據,是與內存打交道的,其背後有分配器支撐其分配內存,基於容器寫好的仿函數封裝成算法,迭代器是算法對容器中數據訪問的橋樑,就是一種泛化的指針。適配器,英文中adapter就是變壓器的意思,用於幫助部件進行轉換,包括容器適配器,仿函數適配器,迭代器適配器。(下圖摘自侯捷的STL源碼剖析一書)
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
int a[6] = {10, 200, 32, 48, 59, 61};
vector<int, allocator<int>> v(a, a+6);
cout << coutnt_if(v.begin(), v.end(), not1(bind2nd(less<int>(), 40)));
return 0;
}
vector:容器
allocator<int>:分配器
v.begin()、v.end():迭代器
count_if:算法
bind2nd:適配器,綁定第2個參數的
not1:適配器