随笔分类 -  c++标准库源码学习

摘要:#include <cstring> template <class Tp> struct ListNode { Tp data; ListNode* next; ListNode* prev; }; template <class Tp> class List { public: List() { 阅读全文
posted @ 2023-06-06 23:33 ho966 阅读(113) 评论(0) 推荐(0)
摘要:std::deque是区别于vector和list的一种有序容器,可以像vector一样高效的取下标,却别于vector, deque可以高效的前插入,自我成长也比vector要高效些。但deque的迭代器操作是没有vector高效的,对deque排序也是异常复杂的,deque的数据结构如下图所示。 阅读全文
posted @ 2021-03-02 11:14 ho966 阅读(305) 评论(0) 推荐(0)
摘要:std::set template <typename Tp> struct Identity_ { const Tp& operator()(const Tp& x) const { return x; } }; template <typename Key, typename Compare = 阅读全文
posted @ 2021-03-01 10:07 ho966 阅读(161) 评论(0) 推荐(0)
摘要:std::map的实现 template <typename Pair> struct Select1st_ { const typename Pair::first_type& operator()(const Pair& x) const { return x.first; } }; templ 阅读全文
posted @ 2021-02-27 22:51 ho966 阅读(309) 评论(0) 推荐(0)
摘要:1、概念: 标准库的std::map、std::mulitmap、std::set、 std::multiset统称为关联式容器,其底层数据结构是基于红黑树实现的。 红黑树作为一种二叉树,满足以下规则: 1)每个节点不是红色就是黑色 2)根节点为黑色 3)如果节点为红色,其子节点必须为黑色 4)任何 阅读全文
posted @ 2021-02-27 16:02 ho966 阅读(162) 评论(0) 推荐(0)
摘要:1 #include "MyHashTable.h" 2 template <typename Tp> 3 struct Identity 4 { 5 const Tp& operator()(const Tp& x) const { return x; } 6 }; 7 template <typ 阅读全文
posted @ 2021-02-25 20:43 ho966 阅读(84) 评论(0) 推荐(0)
摘要:hash_map 1 template <class Pair> 2 struct Select1st 3 { 4 const typename Pair::first_type& operator()(const Pair& x) const 5 { 6 return x.first; 7 } 8 阅读全文
posted @ 2021-02-25 20:42 ho966 阅读(169) 评论(0) 推荐(0)
摘要:c++11之前,标准库里的哈希容器是std::hash_set、std::hash_multiset、std::hash_map、std::hash_multisetmap; c++11之后,他们的名字改成了std::unorder_set、std::unorder_multiset、std::un 阅读全文
posted @ 2021-02-25 20:40 ho966 阅读(136) 评论(0) 推荐(0)
摘要:void Printf(const char* s) { while (*s) { std::cout << *s++; } } template <typename T, typename... Types> void Printf(const char* s, const T& firstArg 阅读全文
posted @ 2021-02-05 13:02 ho966 阅读(153) 评论(0) 推荐(0)
摘要:std::bitset<size_t size> 1、bitset提供了位操作,模板参数就是位的个数; 2、其内部数据结构是long数组,一个long可以表示32位,所以避免浪费,最好取32的倍数 3、bitset提供转std::string的接口,其构造函数入参也支持std::string, 需要 阅读全文
posted @ 2021-02-03 11:30 ho966 阅读(437) 评论(0) 推荐(0)
摘要:使用c++智能指针需要包含头文件<memory>,对于SGI版本的STL, shared_ptr、weak_ptr实现在<bits/shared_ptr.h>中,unique_ptr实现在<bits/unique_ptr.h>中 1、 shared_ptr 作用:通过应用计数实现自动释放指针,用户不 阅读全文
posted @ 2021-01-30 11:02 ho966 阅读(256) 评论(0) 推荐(0)
摘要:以下是基于SGI版本 1、std::string std::string 其实是模板类std::basic_string的实例化,可以在头文件stringfwd.h中查看 std::basic_string的实现可以在basic_string.h和basic_string.tcc文件中查看 2、Co 阅读全文
posted @ 2021-01-14 11:13 ho966 阅读(965) 评论(0) 推荐(0)
摘要:头文件<memory> 1.1 std::allocate、std::deallocate (通过::operator new 和 ::operator delete封装实现) 1.2 std::construct、std::destory (通过placement new 和调用析构函数实现) t 阅读全文
posted @ 2019-11-28 23:59 ho966 阅读(308) 评论(0) 推荐(0)
摘要:STL源码剖析 侯捷 STL主要包括六个组件: 1、配置器:负责空间配置和管理。 2、迭代器:扮演容器和算法之前的胶合剂,所谓“泛型指针”。 3、容器:各种数据结构,如vector,list,set,map等。 4、算法:各种常用算法,如sort,search,copy等。 5、仿函数:一种重载op 阅读全文
posted @ 2019-11-28 23:06 ho966 阅读(224) 评论(0) 推荐(0)