stl源码分析de练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | // StlTest1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <vector> #include <algorithm> #include <iostream> using namespace std; template <typename T> class print { public : void operator ()( const T& elem) { cout << elem << ' ' ; } }; void TmpObjectTest() { int ia[6] = { 0,1,2,3,4,5 }; vector< int > iv(ia, ia+6); for_each(iv.begin(), iv.end(), print< int >()); std::cout << std::endl; } //================================================= template <typename T> class testClass { public : static const int _datai = 5; static const long _datal = 3L; static const char _datac = 'c' ; }; void InClassInit() { std::cout << testClass< int >::_datai << std::endl; std::cout << testClass< int >::_datal << std::endl; std::cout << testClass< int >::_datac << std::endl; } //============================================== class INT { friend ostream& operator <<(ostream& os, const INT& i); public : INT( int i) :m_i(i) {}; INT& operator ++() { ++( this ->m_i); return * this ; } const INT operator ++( int ) { INT temp = * this ; ++(* this ); return temp; } INT& operator --() { --( this ->m_i); return * this ; } const INT operator --( int ) { INT temp = * this ; --(* this ); return temp; } int & operator *() const { return ( int &)m_i; } private : int m_i; }; ostream& operator <<(ostream& os, const INT& i) { os << "[" << i.m_i << "]" ; return os; } void OperatorOverloading() { INT I(5); cout << I++; cout << ++I; cout << I--; cout << --I; cout << *I; } //================================================== int main() { TmpObjectTest(); InClassInit(); OperatorOverloading(); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | // StlTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" //测试 class template 拥有 static data members。 #include <iostream> using namespace std; template <typename T> class testClass { public : static int _data; static T _td; }; int testClass< int >::_data = 1; int testClass< int >::_td = 2; char testClass< char >::_td = 66; void StaticTemplateMemberTest() { std::cout << testClass< int >::_data << std::endl; std::cout << testClass< int >::_td << std::endl; std::cout << testClass< char >::_td << std::endl; } //========================================== template < class I, class O> struct testClass1 { testClass1() { std::cout << "I,O" << std::endl; } }; template < class T> struct testClass1<T*, T*> { testClass1() { std::cout << "T*,T*" << std::endl; } }; template < class T> struct testClass1< const T*, T*> { testClass1() { std::cout << "const T*,T*" << std::endl; } }; void ClassPartialSpecTest() { testClass1< int , char > obj1; testClass1< int *, int *> obj2; testClass1< const int *, int *> obj3; } //============================================ class alloc1 {}; template < class T, class Alloc=alloc1> class vector1 { public : void swap1(vector1<T, Alloc>&) { cout << "swap()" << endl; } }; template < class T, class Alloc1> void swap1(vector1<T, Alloc1>& x, vector1<T, Alloc1>& y) { x.swap1(y); } void FuncTmplPartialOrder() { vector1< int > x, y; swap1(x, y); } //============================================= class alloc2{}; template < class T, class Alloc2=alloc2> class vector2 { public : typedef T value_type; typedef value_type* iterator; template < class I> void insert(iterator position, I first, I last) { std::cout << "insert()" << std::endl; } }; void MemberTemplates() { int ia[5] = { 0,1,2,3,4 }; vector2< int > x; vector2< int >::iterator ite = NULL; x.insert(ite, ia, ia + 5); } //============================================= class alloc3{}; template< class T, class Alloc3 = alloc3,size_t BufSiz = 0> class Deque3 { public : Deque3() { cout << "deque" << endl; } }; template < class T, class Sequence = Deque3<T>> class stack3 { public : stack3() { cout << "stack" << endl; } private : Sequence c; }; void LimitDefaultTemplate() { stack3< int > x; } //============================================ class alloc4 {}; size_t __deque_buf_size(size_t n, size_t sz) { return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1)); } template < class T, class Ref, class Ptr,size_t BufSiz> struct __deque_iterator { typedef __deque_iterator<T, T&, T*, BufSiz> iterator; typedef __deque_iterator<T, const T&, const T*, BufSiz> const_iterator; static size_t buffer_size() { return __deque_buf_size(BufSiz, sizeof (T)); } }; template < class T, class Alloc4 = alloc4,size_t BufSiz = 0> class deque4 { public : typedef __deque_iterator<T, T&, T*, BufSiz> iterator; }; void NonTypeTmplParamBug() { cout << deque4< int >::iterator::buffer_size() << endl; cout << deque4< int , alloc4, 64>::iterator::buffer_size() << endl; } //============================================= int main() { StaticTemplateMemberTest(); ClassPartialSpecTest(); FuncTmplPartialOrder(); MemberTemplates(); LimitDefaultTemplate(); NonTypeTmplParamBug(); return 0; } |
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步