2022年3月28日记:C++11 STL Study Notes
──────────────────────────────────── ┌————————————┐
│▉▉♥♥♥♥♥♥♥♥ 99
C++11 STL Study Notes
- 1、Recommend
- 2、Container
- 3、OOP(Object-Oriented programming) vs GP(Generic Programming)
- 4、Class Templates
-
- Specialization,特化
- Partial Specialization,偏特化
- 5、Perception
1、Recommend
- Doug Lea
- 三本书推荐:《Modern C++ Design》《STL 源码剖析》 《Small Memory Software》
- 《Effecive Modern C++》只讲新东西。
2、Container
- 关联容器适合快速查找,查找速度在纳秒级别,非常快。所以当对数据安插慢可以忍受,但是更看重查找速率,应该先考虑关联容器。
- vector这个容器自动扩充,由分配器去处理这件事。vector是2倍增长
- STL标准库中并没有规定set/map应该用什么实现,但是因为红黑树的性能优异,所以各家编译器都选择了红黑树。红黑树叫做高度平衡搜索树,它能左右调整平衡,这样就不会有最坏的情形关。
set是key-value不分的。 - std::find是循序查找,看运气的。而bserach二分查找找之前需要先sort,sort本身的效率比较低。
list的空间利用率是最好的,但是找起来很慢。 - stack、queue为了保证开发者不去破坏数据的完整性,所以干脆就不提供迭代器。
- hashtable充满了经验值,seperate chain,篮子一定比元素多。链子不能太长,因为循序查询导致速度降低。如果元素不断地加,元素大于篮子,篮子扩大2倍,保证篮子一定比元素多。
3、OOP(Object-Oriented programming) vs GP(Generic Programming)
- OOP的是欲将数据和操作放在与类合一起。
- GP是欲将数据和操作分开来。比如::sort(v.begin(),v.end());算法是通过迭代器操作容器。
- 链表不能使用全局sort排序是因为链表的迭代器不支持random-access,所以只能用自己的成员函数进行排序。
4、Class Templates
template<typename T> class complex { public:cpmplex(T r=0,T i=0):re(r),im(i){}complex& operator +=(const complex &);T real() const {return re;}T imag() const {return im;}private:T re,im;friend complex& __doapl(complex *,const complex &); };complex<double> c1(2.5,1.5);complex<int> c2(2.6);
template<typename T>inline const T& min(const T&a,const T& b){ return b<a ? b:a;}class stone{public:stone(int w,int h,int we):_w(w),_h(h),weight(we){}bool operator<(const stone & rhs) const{return _weight<rhs._weight;}private:int _w,_h,_weight;};
Specialization,特化
template<typename Key> struct hash{};typdef template<> __STL_TEMPLATE_NULL __STL_TEMPLATE_NULL struct hash<char>{ size_t operator()(char x) const {return x;} };__STL_TEMPLATE_NULL struct hash<short>{size_t operator()(short x)const {return x;}};__STL_TEMPLATE_NULL struct hash<unsigned short>{ size_t operator()(int x)const {return x;} }; __STL_TEMPLATE_NULL sturct hash<unsign int>{size_t opertator()(unsign int){return x;}};__STL_TEMPLATE_NULL sturct hash<long>{size_t opertator()(long{return x;}};
Partial Specialization,偏特化
//数量上的偏特化template <typename T,typename Alloc=alloc>class vector{//...};template\<typename Alloc>{//如果是bool类型的话用更精简的空间来表现它...};
//范围上的偏特化template<typename Iterator>struct iterator_traits{typedef typename Iterator::iterator_catetory iterator_categorytypedef 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 random_access_iterator_tag iterator_category;typedef T value_type;typedef ptrdiff_t difference_type;typedef T* pointer;typedef T& reference;};template<typename T>struct iterator_traits<const T*>{typedef random_access_iterator_tag iterator_category;typedef T x value-type;typedef ptrdiff_t __STL_TEMPLATE_NULL;typedef const T* pointer;typedef const T& reference;};
5、Perception
通过今天的学习,我对STL容器有了一定了解,相信自己在以后的工作中都是经过深思熟虑后选择的容器,而不是一个草率的决定。然后对一些编程的习惯上认识到了自己的不足,希望在未来可以写出优质的代码。