摘要: 这段时间看C++标准库,看到有一个求公共类型的实用函数common_type<>;其实现是如下:template<typename T1,typename T2> struct common_type<T1,T2> { typedef decltype(true?declval<T1>(),declval<T2>()) type; }刚开始还觉得奇怪,条件永远为true,不就是直接计算T1吗,想当然的以为产生公共类型不就是T1的类型吗?后然通过一个实例上机实验才发现不是这么回事。于是找到关于条件运算符的说明;我们知道条件运算符?:是C 阅读全文
posted @ 2013-04-12 13:54 xuning2516 阅读(328) 评论(0) 推荐(0) 编辑
摘要: C++11 提供了许多的类型特征和型别实用函数。1 declval01122 /// declval 01123 template<typename _Tp> 01124 struct __declval_protector 01125 { 01126 static const bool __stop = false; 01127 static typename add_rvalue_reference<_Tp>::type __delegate(); 01128 }; 01129 01130 template<typename _... 阅读全文
posted @ 2013-04-12 10:09 xuning2516 阅读(306) 评论(0) 推荐(0) 编辑
摘要: C++11提供了compile_time fractions andcompile-time rational arithmetic support。支持编译时常量。头文件 <ratio>来看ratio是怎么定义的00152 template<intmax_t _Num, intmax_t _Den = 1> 00153 struct ratio 00154 { 00155 static_assert(_Den != 0, "denominator cannot be zero"); 00156 static_assert(_Num >= -_ 阅读全文
posted @ 2013-04-09 20:14 xuning2516 阅读(517) 评论(0) 推荐(0) 编辑
摘要: 表示时间方法是时间的基点timepoint+duaration的形式;而变表示时间的类型有C tm结构体struct tm { int tm_sec; /*秒,正常范围0-59, 但允许至61*/ int tm_min; /*分钟,0-59*/ int tm_hour; /*小时, 0-23*/ int tm_mday; /*日,即一个月中的第几天,1-31*/ int tm_mon; /*月, 从一月算起,0-11*/ 1+p->tm_mon; int tm_year; /*年, 从1900至今已经多少年*/ 1900+ p->tm... 阅读全文
posted @ 2013-04-09 09:58 xuning2516 阅读(146) 评论(0) 推荐(0) 编辑
摘要: forward_list 源码学习//forward_list的迭代器是forward_iterator,因此在forward list中需要注意保存前面一个元素的iterator,方便插入和删除。auto posbefore=flist.before_begin();for(auto pos=flist.begin();pos!=flist.end();++pos,++posbefore)例如在remove_if的成员函数 ,删除的是当前元素的后一个元素。template <class _Tp, class _Alloc> template <class _Predicat 阅读全文
posted @ 2013-03-31 21:53 xuning2516 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 在stl中既有通用函数,又有相同成员函数主要表现在list中。以remove为例 list<int> coll; // insert elements from 6 to 1 and 1 to 6 for (int i=1; i<=6; ++i) { coll.push_front(i); coll.push_back(i); } // print all elements of the collection cout << "pre: "; copy (coll.cbegin(), coll.cend... 阅读全文
posted @ 2013-03-31 21:36 xuning2516 阅读(309) 评论(0) 推荐(0) 编辑
摘要: 本文来自http://www.cplusplus.com/reference/stl/Member mapThis is a comparison chart with the different member functions present on each of the different containers:Legend:C++98Available since C++98C++11New in C++11Sequence containersHeaders<array><vector><deque><forward_list>< 阅读全文
posted @ 2013-03-31 16:58 xuning2516 阅读(205) 评论(0) 推荐(0) 编辑
摘要: C++11中新增了forward_list,头文件是<forward_list>这个container是一个单向链表,在sgi stl中对应的是slist数据结构中数据项保存的是头节点,尾节点初始化为0,表示链表的end()。template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > class slist : private _Slist_base<_Tp,_Alloc> { // requirements: __STL_CLASS_REQUIRES(_Tp, _Assignable 阅读全文
posted @ 2013-03-31 16:50 xuning2516 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 在sgi stl的实现版本中看到关于size member的实现。size_type size() const { size_type __result = 0; distance(begin(), end(), __result); return __result; }这里的distance是一个全局函数。对于random_access iterator 而言是两个迭代器相减,具有O(1)的复杂度,而对于其他类型的迭代器则是++first到last的形式,具有O(n)的复杂度。 Why islist<>::size()linear time?Thesize()member... 阅读全文
posted @ 2013-03-31 13:16 xuning2516 阅读(259) 评论(0) 推荐(0) 编辑