1.STL六大组件的功能和运用
1.容器
2.算法
3.迭代器
4.仿函数
5.配接器:一种用来修饰容器、仿函数、迭代器的东西
6.配置器:负责空间配置与管理
find() 、for_each()函数的范围为【first,last)左闭右开
关于如何对于自定义类自增自减和输出
#include<iostream>
using namespace std;
class INT {
friend ostream& operator<<(ostream& o, const INT& a);
private:
int m_i;
public:
INT(int i):m_i(i){}
INT& operator++() {
++(this->m_i);
return *this;
}
//i++
const INT operator++(int) {
INT temp = *this;
++(*this);
return temp;
}
INT& operator--() {
--(this->m_i);
return*this;
}
//i--
const INT operator--(int) {
INT temp = *this;
--(*this);
return temp;
}
int& operator*() {
return (int&)m_i;
}
};
ostream& operator<<(ostream& o, const INT& a) {
o << "[" << a.m_i << "]" << endl;
return o;
}
void test1(){
INT a(5);
cout << a++ << endl;
cout << ++a << endl;
cout << a-- << endl;
cout << --a << endl;
cout << *a << endl;
}
仿函数
template<class T>
class Plus
{
public:
T operator()(const T& a, const T& b) const{
return a + b;
}
};
template<class T>
class Minus
{
public:
T operator()(const T& a, const T& b)const {
return a - b;
}
};
void test2() {
Plus<int>a;
Minus<int>b;
cout << a(1, 2) << endl;
cout << b(1, 2) << endl;
cout << Plus<int>()(1, 2) << endl;
cout << Minus<int>()(1, 2) << endl;
}
空间适配器
#ifndef DEFALLOC_H
#define DEFALLOC_H
#include<iostream>
#include<new>
#include<cstddef>
#include<cstdlib>
#include<climits>
template<class T>
inline T* _allocate(ptrdiff_t size, T*) {
set_new_handle(0);
T* tem = (T*)(::operator new((size_t)(size * sizeof(T))));
if (tem == 0) {
std::cout << "out of memory" << std::endl;
exit(1);
}
return tem;
}
template<class T>
inline void deallocate(T* buffer) {
::operator delete(buffer);
}
template<class T>
class allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
pointer allocate(size_type n) {
return ::allocate((difference_type)n, (pointer)0);
}
void deallocate(pointer p) {
::deallocate(p);
}
pointer address(reference x) {
return (pointer)&x;
}
const_pointer const_address(const_reference x) {
return (const_pointer)&x;
}
size_type init_page_size() {
return max(size_type(1), size_type(4096 / sizeof(T)));
}
size_type max_size()const {
return max(size_type(1), size_type(UINT_MAX / sizeof(T)));
}
};
class allocator<void>
{
public:
typedef void* pointer;
};
#endif // !DEFALLOC_H
分配内存和对象构造
关于配置器
二级适配器中当申请的区块够大(大于128bytes时,移交给一级适配器)小于128bytes时以内存池管理则在十六个对应的小额区块链表中取出一块进行分配。
内存基本处理工具
函数会调用construct(&*(result + last - i),*i);
注:POD类型是个普通的类型,在C++中常见的类型都有这样的属性,而不像一些存在着虚函数虚继承的类型那么特别,并且对于C兼容
POD解释:
1、 所有标量类型(基本类型和指针类型)、POD结构类型、POD联合类型、以及这几种类型的数组、const/volatile修饰的版本都是POD类型。
2、 POD结构/联合类型:一个聚合体(包括class),它的非static成员都不是pointer to class member、pointer to class member function、非POD结构、非POD联合,以及这些类型的数组、引用、const/volatile修饰的版本;
并且,此聚合体不能有用户自定义的构造函数、析构函数、拷贝构造函数.
可参考连接:https://www.cnblogs.com/liugangBlog/p/6472381.html
会调用construct(&*i,x);
迭代器相应类型
traits编程技法
//类型推导
template<class I,class T>
void fun_ctl(I& a,T& b){
//主逻辑
}
template<class T>
void fun(T& a){
fun_ctl(a,*a);
}
int main(){
int number = 100;
fun(number);
}
1.value type:所指对象类型
2.difference type:两个迭代器之间的距离
3.reference type:所指对象的引用
int * pi = new int;//此时的reference type 应该是int&
const int * pic = new int;//此时的reference type 应该是const int&
4.pointer:所指对象的指针
5.iterator_category:迭代器按照移动类型和施行操作分为下面五类只读,只写,允许“写入型算法”,双向迭代器,第五种涵盖所有指针的算术能力(1~3种只能++,第四种多了–,第五种可以p+n , p-n , p[n] , p1-p2 , p1<p2)
//用于萃取出迭代器类型来进行traits偏特化
#include<iostream>
template<class T>
inline typename iterator_traits<T>::iterator_category
iterator_category(const T&) {
typedef typename iterator_traits<T>::iterator_category category;
return category();
}
//STL 提供的iterators class
template<class T,
class Category,
class Distance = ptrdiff_t,
class Pointer = T*,
class Reference = T&>
struct iterator{
typedef Category iterator_category;
typedef Distance difference_type;
typedef T type_value;
typedef Poiter pointer;
typedef Reference reference;
};
/*
ctor:构造函数
copy:拷贝构造
assignment:赋值函数
dtor:析构函数
当满足显式定义这四种函数、类中有非静态非POD的数据成员、有基类,三种情况之一时,上面四种函数就称之为non-trivial(有意义)函数
POD:plain old data指传统c结构体类型或者c++内建类型
*/
---------------
参考书籍:《STL源码剖析》——侯捷著