STL:源码剖析(部分)—— 目录

侯捷——STL源码剖析 笔记 在下图中,我们使用了如下: 1.一个容器vector 2.使用vector时,使用分配器分配内存 3.使用vi.begin(),vi.end()即迭代器,作为算法的参数 4.使用count_if算法 5.使用仿函数less() 6.使用函数适配器来对我们算法的结果进行进一步筛选(not1, bind2nd) 迭代器是一个左开右闭的区间,也就是说迭代器的end是最后一个元素的下一个元素。 序列式容器特点额外学习材料array一段连续空间,不论是否使用,都会全部占用arrayvector尾部可进可出,当空间不够 阅读详情

迭代器

迭代器剖析

my_iterator.h

#ifndef MY_ITERATOR_H
#define MY_ITERATOR_H

namespace Srh
{

typedef int ptrdiff_t;

struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};

template<typename _C, typename _Ty, typename _D = ptrdiff_t,
	typename _Pointer = _Ty*, typename _Reference = _Ty&>
	struct iterator
{
	typedef _C			iterator_category;
	typedef _Ty			value_type;
	typedef _D          difference_type;
	typedef _Pointer    pointer;
	typedef _Reference  reference;
};

// 类型萃取
template<typename _Iterator>
struct iterator_traits
{
	//ietrator_traits() {}

	typedef typename _Iterator::iterator_category   iterator_category;
	typedef typename _Iterator::value_type			value_type;
	typedef typename _Iterator::difference_type		difference_type;
	typedef typename _Iterator::pointer				pointer;
	typedef typename _Iterator::reference			reference;
};

// 原生指针 (偏特化版本)
template<typename T>
struct iterator_traits<T*>
{
	typedef typename random_access_iterator_tag   iterator_category;
	typedef typename T							  value_type;
	typedef typename ptrdiff_t					  difference_type;
	typedef typename T* pointer;
	typedef typename T& reference;
};

// 常性指针(偏特化版本)
template<typename T>
struct iterator_traits<const T*>
{
	typedef typename random_access_iterator_tag   iterator_category;
	typedef typename T							  value_type;
	typedef typename ptrdiff_t					  difference_type;
	typedef typename const T* pointer;
	typedef typename const T& reference;
};

// 决定某个迭代器类型
template<typename _Iterator>
inline typename iterator_traits<_Iterator>::iterator_category
iterator_category(const _Iterator&) {
	typedef typename iterator_traits<_Iterator>::iterator_category category;
	return category();
}

// 决定某个迭代器的distance_type
template<typename _Iterator>
inline typename iterator_traits<_Iterator>::difference_type*
distance_type(const _Iterator&) {
	return static_cast<typename iterator_traits<_Iterator>::difference_type*>(0);
}

// 决定某个迭代器的value_type
template<typename _Iterator>
inline typename iterator_traits<_Iterator>::value_type*
value_type(const _Iterator&) {
	return static_cast<typename iterator_traits<_Iterator>::value_type*>(0);
}

// 正向迭代器
template<typename _Ty, typename _D = ptrdiff_t>
struct _Forit : public iterator<forward_iterator_tag, _Ty, _D> {};

// 双向迭代器
template<typename _Ty, typename _D = ptrdiff_t>
struct _Bidit : public iterator<bidirectional_iterator_tag, _Ty, _D> {};

// 随机迭代器
template<typename _Ty, typename _D = ptrdiff_t>
struct _Ranit : public iterator<random_access_iterator_tag, _Ty, _D> {};

// advance
template<typename _II, typename _D>
inline void __advance(_II& i, _D n, input_iterator_tag)
{
	while (n--)
	{
		i++;
	}
}

template<typename _BI, typename _D>
inline void __advance(_BI& i, _D n, bidirectional_iterator_tag)
{
	if (n >= 0)
	{
		while (n--) ++i;
	}
	else
	{
		while (n++) --i;
	}
}

template<typename _RAI, typename _D>
inline void __advance(_RAI& i, _D n, random_access_iterator_tag)
{
	i += n;
}

template<typename _II, typename _D>
inline void advance(_II& i, _D n)
{
	iterator_traits<_II>();
	typedef typename iterator_traits<_II>::iterator_category cate;
	__advance(i, n, cate());
}

template<typename _II>
inline typename iterator_traits<_II>::difference_type
__distance(_II _F, _II _L, input_iterator_tag)
{
	typename iterator_traits<_II>::difference_type n = 0;
	while (_F != _L)
	{
		_F++;
		n++;
	}
	return n;
}

template<typename _RAI>
inline typename iterator_traits<_RAI>::difference_type
__distance(_RAI _F, _RAI _L, random_access_iterator_tag)
{
	return _L - _F;
}

template<typename _II>
inline typename iterator_traits<_II>::difference_type
distance(_II _F, _II _L)
{
	return __distance(_F, _L, iterator_category(_F));
}

}

#endif

类型萃取

类型萃取剖析

my_type_traits.h

#ifndef MY_TYPE_TRAITS_H
#define MY_TYPE_TRAITS_H

namespace Srh
{
	struct __true_type {};
	struct __false_type {};

	template<typename type> 
	struct __type_traits
	{
		typedef __true_type  this_dummy_member_must_be_first;
		typedef __false_type  has_trivial_default_constructor;
		typedef __false_type  has_trivial_copy_constructor;
		typedef __false_type  has_trivial_assignment_operator;
		typedef __false_type  has_trivial_destructor;
		typedef __false_type  is_POD_type; 
	};

	// 特化版本
	template<> struct __type_traits<char>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;  
	};

	template<> struct __type_traits<signed char>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};

	template<> struct __type_traits<unsigned char>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;  
	};

	template<> struct __type_traits<short>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};

	template<> struct __type_traits<unsigned short>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<int>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<unsigned int>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<long int>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<unsigned long int>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};

	template<> struct __type_traits<long long>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<unsigned long long>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<> struct __type_traits<float>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};

	template<> struct __type_traits<double>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};

	template<> struct __type_traits<long double>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
	template<class T>
	struct __type_traits<T*>
	{
		typedef __true_type  has_trivial_default_constructor;
		typedef __true_type  has_trivial_copy_constructor;
		typedef __true_type  has_trivial_assignment_operator;
		typedef __true_type  has_trivial_destructor;
		typedef __true_type  is_POD_type;   
	};
}

#endif

构造和析构

源码剖析

my_construct.h

#ifndef MY_CONSTRUCT_H
#define MY_CONSTRUCT_H

#include"my_iterator.h"
#include"my_type_traits.h"

namespace Srh
{

// 包装定位new placement new
template<typename T1, typename T2>
inline void construct(T1* p, const T2& val)
{
	new (p) T1(val);
}

// 无参的
template<typename T>
inline void construct(T* p)
{
	new (p) T();
}

// 析构对象
template<typename T>
inline void destroy(T* p)
{
	p->~T();
}

/*
// 析构范围内的对象
template<typename _FI>
inline void destroy(_FI _F, _FI _L)
{
	for (; _F != _L; ++F)
	{
		destroy(&*_F);
		//(*_F)表示迭代器所指之物, 
		// (&*_F)表示迭代器所指之物的地址  
	}
}
*/

// 如果析构函数无关紧要,那么就不需要上面这样麻烦
// 那么利用上面已知的true/false来判断
template<typename _FI>
inline void __destroy_aux(_FI _F, _FI _L, Srh::__true_type)
{}

template<typename _FI>
inline void __destroy_aux(_FI _F, _FI _L, Srh::__false_type)
{
	for (; _F != _L; ++_F)
	{
		destroy(&*_F);
	}
}

template<typename _FI, typename T>
inline void __destroy(_FI _F, _FI _L, T*)
{
	//cout << typeid(T).name() << endl;
	//Srh::__type_traits<T>();
	// 得知T所指之物的值类型
	// 再根据类型萃取 获取 值类型的析构函数是 true还是false
	typedef typename Srh::__type_traits<T>::has_trivial_destructor dest;
	__destroy_aux(_F, _L, dest());
}

template<typename _FI>
inline void destroy(_FI _F, _FI _L)
{
	// 调用下面的__destroy,获取_F迭代器所指之物的类型
	__destroy(_F, _L, Srh::value_type(_F));
}

}
#endif

空间配置器

第一级配置器

第二级配置器

my_alloc.h

#ifndef MY_ALLOC_H
#define MY_ALLOC_H

#include<iostream>
using namespace std;

namespace Srh
{

#if 0
#include<new>
#define __THROW_BAD_ALLOC throw std::bad_alloc;
#elif !defined(__THROW_BAD_ALLOC)
#define __THROW_BAD_ALLOC std::cerr << "out of memory" << std::endl; exit(1);
#endif


// 第一级配置器
template<int inst>
class __malloc_alloc_template
{
public:
	using PFUN = void (*)();

private:
	// 处理内存不足问题
	static void* oom_malloc(size_t n)
	{
		void* result = nullptr;
		void (*my_malloc_handler) () = nullptr;

		// 要么得到空间,要么终止程序
		for (; ;) // 不断尝试 释放、配置、再释放、再配置...
		{
			my_malloc_handler = __malloc_alloc_oom_handler;
			if (nullptr == my_malloc_handler)
			{
				__THROW_BAD_ALLOC;
			}
			my_malloc_handler();	//调用处理例程,企图释放内存
			result = malloc(n);		// 再次尝试配置内存
			if (nullptr != result)
			{
				return result;
			}
		}
	}

	static void* oom_realloc(void* p, size_t new_sz)
	{
		void* result = nullptr;
		void (*my_malloc_handler) () = nullptr;

		// 要么得到空间,要么终止程序
		for (; ;) // 不断尝试 释放、配置、再释放、再配置...
		{
			my_malloc_handler = __malloc_alloc_oom_handler;
			if (nullptr == my_malloc_handler)
			{
				__THROW_BAD_ALLOC;
			}
			my_malloc_handler();	//调用处理例程,企图释放内存
			result = realloc(p, new_sz);	// 再次尝试配置内存
			if (nullptr != result)
			{
				return result;
			}
		}
	}
	
	//static void(*__malloc_alloc_oom_handler)();
	static PFUN __malloc_alloc_oom_handler;

public:
	static void* allocate(size_t n)   
	{
		void* result = malloc(n);	// malloc
		if (nullptr == result)
		{
			// 无法满足需求,采用oom_malloc
			result = oom_malloc(n);
		}
		return result;
	}
	static void deallocate(void* p, size_t n)   
	{
		free(p);	// free
	}

	static void* reallocate(void* p, size_t old_sz, size_t new_sz)	 
	{
		void* result = realloc(p, new_sz);	// realloc
		if (nullptr == result)
		{
			// 无法满足需求,采用oom_realloc
			result = oom_realloc(p, new_sz);
		}
		return result;
	}

	//static void (*set_malloc_handler(void (*f))();
	static PFUN set_malloc_handler(PFUN p)
	{
		PFUN old = __malloc_alloc_oom_handler;
		__malloc_alloc_oom_handler = p;
		return old;
	}

};

/*
template<int inst>
void(*__malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = nullptr;
*/
template<int inst>
typename __malloc_alloc_template<inst>::PFUN
__malloc_alloc_template<inst>::__malloc_alloc_oom_handler = nullptr;

// 将参数inst置为0
using malloc_alloc = __malloc_alloc_template<0>;


// 第二级配置器
enum { __ALIGN = 8 };	// 小型区块的上调边界
enum { __MAX_BYTES = 128 };	// 小型区块的上限
enum { __NFREELISTS = __MAX_BYTES / __ALIGN };	// free_lists个数

template<bool threads, int inst>
class __default_alloc_template
{
private:
	// 链表
	union obj
	{
		union obj* free_list_link;	// next;
		char client_data[1];
	};

private:
	// 一个指针数组,自由链表
	static obj* volatile free_list[__NFREELISTS];

	static size_t ROUND_UP(size_t bytes) // 1~8 / 9~16...
	{
		return (bytes + __ALIGN - 1) & ~(__ALIGN - 1);
	}
	static size_t FREELIST_INDEX(size_t bytes)
	{
		return (bytes + __ALIGN - 1) / __ALIGN - 1;
	}

	static char* start_free;	// 内存池起始位置
	static char* end_free;		// 内存池结束位置
	static size_t heap_size; // total

	// 配置一大块空间,可以容纳nobjs个size大小的区块
	// 如果配置nobjs个区块有所不便,nobjs会做出相应改变
	static char* chunk_alloc(size_t size, int& nobjs)
	{
		char* result = nullptr;
		// 需要空间的总大小
		size_t total_bytes = size * nobjs;
		// 内存池剩余空间
		size_t bytes_left = end_free - start_free;

		// 如果内存池剩余空间满足需要空间
		if (bytes_left >= total_bytes)
		{
			result = start_free;
			start_free = start_free + total_bytes;
			return result;
		}
		else if (bytes_left >= size)
		{
			// 如果剩余空间只够1个以上的块(但小于总需求)
			nobjs = bytes_left / size;
			result = start_free;
			start_free = start_free + total_bytes;
			return result;
		}
		else
		{
			// 如果连一块空间都不够
			size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4);
			
			// 将剩下的一点点空间再利用
			if (bytes_left > 0)
			{
				// 将剩余空间配置给合适的free_list
				obj* volatile* my_free_list = free_list + FREELIST_INDEX(bytes_left);
				((obj*)start_free)->free_list_link = *my_free_list;
				*my_free_list = (obj*)start_free;
			}

			// 配置heap空间,补充内存池
			start_free = (char*)malloc(bytes_to_get);
			if (nullptr == start_free)
			{
				obj* volatile* my_free_list = nullptr;
				obj* p = nullptr;
				for (int i = size; i <= __MAX_BYTES; i += __ALIGN)
				{
					my_free_list = free_list + FREELIST_INDEX(i);
					p = *my_free_list;
					if (nullptr != p)
					{
						*my_free_list = p->free_list_link;
						start_free = (char*)p;
						end_free = start_free + i;
						return chunk_alloc(size, nobjs);
					}
				}
				end_free = 0;
				start_free = (char*)malloc_alloc::allocate(bytes_to_get);
			}

			// 修正内存池的结束位置和总大小
			end_free = start_free + bytes_to_get;
			heap_size += bytes_to_get;
			// 递归调用自己,修正nobjs
			return chunk_alloc(size, nobjs);
		}
	}

	// 返回一个大小为size的对象,
	// 并可能加入大小为size的其他区块到free_list中
	static void* refill(size_t size)
	{
		int nobjs = 20;
		char* chunk = chunk_alloc(size, nobjs);
		if (1 == nobjs) return chunk;

		obj* volatile* my_free_list = nullptr;
		obj* result = (obj*)chunk;
		obj* current_obj = nullptr;
		obj* next_obj = nullptr;
		int i = 0;
		
		my_free_list = free_list + FREELIST_INDEX(size);
		*my_free_list = next_obj = (obj*)(chunk + size);
		for (i = 1; ; ++i)
		{
			current_obj = next_obj;
			next_obj = (obj*)((char*)next_obj + size);
			if (i == nobjs - 1)
			{
				current_obj->free_list_link = nullptr;
				break;
			}
			current_obj->free_list_link = next_obj;
		}
		return result;
	}

public:
	static void* allocate(size_t size)
	{
		if (size > (size_t)__MAX_BYTES)
		{
			return malloc_alloc::allocate(size);
		}
		
		obj* result = nullptr;
		obj* volatile* my_free_list = nullptr;
		my_free_list = free_list + FREELIST_INDEX(size);
		result = *my_free_list;
		if (nullptr == result)
		{
			void* r = refill(ROUND_UP(size));
			return r;
		}
		*my_free_list = result->free_list_link;
		return result;
	}

	static void deallocate(void* p, size_t n)
	{
		if (n > (size_t)__MAX_BYTES)
		{
			return malloc_alloc::deallocate(p, n);
		}

		obj* q = (obj*)p;
		obj* volatile* my_free_list = nullptr;
		// 寻找相应的free_list
		my_free_list = free_list + FREELIST_INDEX(n);
		// 头插法,回收区块
		q->free_list_link = *my_free_list;
		*my_free_list = q;
	}

	static void* reallocate(void* p, size_t old_sz, size_t new_sz)
	{
		if (old_sz > (size_t)__MAX_BYTES && new_sz > (size_t)__MAX_BYTES)
		{
			return malloc_alloc::reallocate(p, old_sz, new_sz);
		}
		if (ROUND_UP(old_sz) == ROUND_UP(new_sz))
		{
			return p;
		}

		size_t sz = old_sz < new_sz ? old_sz : new_sz;
		void* s = allocate(new_sz);
		memmove(s, p, sz);
		deallocate(p, old_sz);
		return s;
	}


};

// 对各个成员初始化
template<bool threads, int inst>
typename __default_alloc_template<threads, inst>::obj* volatile
__default_alloc_template<threads, inst>::free_list[__NFREELISTS] = {};

template<bool threads, int inst>
char* __default_alloc_template<threads, inst>::start_free = nullptr;

template<bool threads, int inst>
char* __default_alloc_template<threads, inst>::end_free = nullptr;

template<bool threads, int inst>
size_t __default_alloc_template<threads, inst>::heap_size = 0;

//////////////////////////////////////
//////////////////////////////////////
// SGI STL
#ifdef __USE_MALLOC
typedef __malloc_alloc_template<0> malloc_alloc;
typedef malloc_alloc alloc;
#else
typedef __default_alloc_template<0, 0> alloc;
#endif

template<typename T, typename Alloc>
class simple_alloc
{
public:
	// 申请
	static T* allocate(size_t n) // n个T类型的
	{
		return Alloc::allocate(sizeof(T) * n);
	}
	static T* allocate()
	{
		return Alloc::allocate(sizeof(T));
	}

	// 删除函数
	void deallocate(T* p, size_t n)
	{
		if (nullptr == p) return;
		Alloc::deallocate(p, size(T) * n);
	}
	void deallocate(T* p)
	{
		if (nullptr == p) return;
		Alloc::deallocate(p, sizeof(T));
	}
};

}
#endif
STL源码剖析 复盘STL-2 阅读详情

相关推荐

STL源码剖析——STL算法之merge合并算法

本文介绍的STL算法中的merge合并算法源码中介绍了函数merge、inplace_merge。并对这些函数的源码进行详细的剖析,并适当给出使用例子。

关注校招求职,微信号:job_campus 3149

STL源码剖析.pdf 简体中文版

侯捷的大作 STL的巨擎 源码之前了无秘密

STL源码剖析(侯杰)——读书笔记

文章目录STL源码剖析(侯杰)——读书笔记1. STL概论2.空间配置器SGI标准空间配置器, std::allocatorSGI特殊的空间配置器, std::alloc构造和析构 construct()和destroy()空间配置与释放3.迭代器概念与traits编程概念迭代器相应型别传递Traits编程技法——STL源码门钥Partial Specialization(偏特化)的意义std::iterator的保证4.序列式容器vector**list**dequestackqueueheappri

风源- 5228

STL源码剖析》深入剖析理解

STL源码剖析》是由侯捷所著,是一本经典的C++ STL源码解析书籍,它深入剖析SGI版本的C++ STL源代码,解释了STL各个组件的实现原理和设计思路。

qq_51969153的博客 1073

STL源码刨析

1. STL概述 STL起源: 为的就是复用性的提升,减少人力资源的浪费,建立了数据结构算法的一套标准。 STL所实现的、是依据泛型思维架设起来的一个概念结构。这个以抽象概念〔 abstract concepts)为主体而非以实际类(classes)为主体的结构,形成了一个严谨的接口标准。在此接口之下,任何组件都有最大的独立性,并以所谓迭代器〈 iterator)胶合起来,或以所谓配接器(adapter)互相配接,或以所谓仿函数( functor)动态选择某种策略( policy或strategy)

frighting_ing的博客 8248

STL源码剖析】从源码看 list:从迭代器到算法

本文深入剖析STL中list容器的实现细节,适合已掌握C++模板和STL基础的开发者学习。文章从六个方面展开:概述list的优劣势;分析节点结构为双向循环链表;探讨迭代器设计与操作重载;揭示循环双向链表的数据结构;讲解构造与内存管理机制;介绍常用接口实现。重点解析了list通过哨兵节点实现"前闭后开"的特性,以及其特有的内存分配方式。文章通过源码分析,展示了STL中list的高效实现技巧,为开发者深入理解STL容器和数据结构提供了宝贵参考。

2401_87944878的博客 7893

STL源码剖析】从源码看 vector:底层扩容逻辑与内存复用机制

本文深入剖析STL中vector容器的实现原理,主要从四个方面展开:1)数据结构采用三个指针管理动态数组;2)迭代器直接使用原生指针实现;3)构造过程通过allocate_and_fill分配初始化空间;4)元素操作如push_back采用二倍扩容策略。帮助读者理解STL底层实现的价值,vector通过封装动态数组管理细节,为用户提供高效易用的接口。分析显示,vector的核心在于动态内存管理和高效扩容机制,适合已有STL基础的开发者深入理解其实现原理。

2401_87944878的博客 6569

STL源码剖析总结

记录侯捷《STL源码剖析》感悟学习

qq_42579966的博客 1982

STL源码剖析——STL算法stl_algo.h

本文剖析STL算法源码,在剖析源码的同时会给出一些例子,这样加深对其理解,方便我们对这些算法的使用。

关注校招求职,微信号:job_campus 3978

STL源码剖析——STL算法之remove删除算法

本文介绍的STL算法中的remove删除算法源码中介绍了函数remove、remove_copy、remove_if、remove_copy_if、unique、unique_copy。并对这些函数的源码进行详细的剖析,并适当给出使用例子。

关注校招求职,微信号:job_campus 2023

STL源码分析目录

SIG STL源码分析 前言 本专栏主要以STL源码剖析分析路线来分析SIGSTL3.0源码. 整个模块准备对学习STL源码剖析之后做一个系统的总结, 这些都是我个人的理解, 如果分析有什么问题欢迎各位大佬们指出. 也很感谢作者以及网络中各个大佬的总结, 让我也能更容易更深刻的理解到STL强大和方便, 也让我对template感受深刻. 以下是我自己对STL版块进行分析. 总共分为六个版块 : 空...

啦啦啦 3632

STL源码剖析——STL算法之find查找算法

本文介绍的STL算法中的find、search查找算法。在STL源码中有关算法的函数大部分在本文介绍,包含findand find_if、adjacent_find、search、search_n、lower_bound、 upper_bound、 equal_range、binary_search、find_first_of、find_end相关算法,下面对这些算法源码进行了详细的剖析,并且适当给出应用例子,增加我们对其理解,方便我们使用这些算法

关注校招求职,微信号:job_campus 4052

STL源码剖析简体中文完整版学习资源:深度掌握STL内部实现

STL源码剖析简体中文完整版学习资源:深度掌握STL内部实现 【下载地址】STL源码剖析简体中文完整版学习资源 探索STL源码的奥秘,提升编程技能!本资源提供《STL源码剖析简体中文完整版》清晰扫描PDF,深入解析STL的容器、迭代器、算法等核心组件。通过学习,您将掌握vector、list、heap等容器的实现,理解红...

gitblog_06720的博客 1011

STL源码剖析】读书笔记

跳过吧在使用迭代器去“撮合”算法和容器时,需要知道容器对象的类型。由此,首先引入了function template参数推导机制,该方法解决了函数形参的推导,但是无法推导出函数的返回值类型。接着有介绍了class的内嵌类型声明,在类(或结构体)中通过typedef T value_type取得参数类型,然后让返回值类型为typename T::value_type获得T的参数类型,但这种方法只在class对象中有效,在原生指针中无法定义内嵌类型。此时,本章最重要的traits特性萃取机就闪亮登场了。

qq_47461884的博客 1326

STL源码剖析STL六大组件功能与运用(目录

简介: 各种数据结构,用来存放数据,如vector、list、deque(双端队列)、set、map等等【STL源码剖析】容器(待补充)简介: 各种STL提供的常用算法,如sort、search、copy、erase等等。从实现的角度来看,STL算法是一种方法模板(function template)【STL源码剖析算法(待补充)简介: 迭代器扮演的是容器和算法之间的粘合剂,是所谓的“泛型指针”。从实现的角度来看,迭代器是一种将operator*,operator->,operator++,operato

cloud的博客 1036

STL源码剖析——关联容器之map

前言

关注校招求职,微信号:job_campus 3637
上一篇: C++习题:模板函数如何得知参数是智能指针还是裸指针
下一篇: 《最近的感想》
_索伦
博客等级 码龄5年 467粉丝 · 278原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_索伦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值