Advertisement
Guest User

szablony zajecia

a guest
May 27th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. template<typename T>
  7. T minimum(T a, T b)
  8. {
  9. return a < b ? a : b;
  10. }
  11. template<>
  12. const char* minimum(const char* a, const char* b)
  13. {
  14. return strcmp(a, b) < 0 ? a : b;
  15. }
  16. template<typename T>
  17. class MyArray
  18. {
  19. private:
  20. int m_iSize;
  21. T *m_array;
  22. public:
  23. MyArray();
  24. MyArray(const MyArray<T> &a);
  25. ~MyArray();
  26. T &operator[](int index);
  27. MyArray &operator=(const MyArray<T> &a);
  28. inline int GetSize(){ return m_iSize; }
  29. };
  30. template<typename T>
  31. MyArray<T>::MyArray()
  32. :m_iSize(1)
  33. {
  34. m_array = new T[1];
  35. m_array[0] = T();
  36. }
  37. template<typename T>
  38. MyArray<T>::~MyArray()
  39. {
  40. delete[]m_array;
  41. }
  42. template<typename T>
  43. MyArray<T>::MyArray(const MyArray<T> &a)
  44. {
  45. m_iSize = a.m_iSize;
  46. m_array = new T[m_iSize];
  47. for(int i = 0; i < m_iSize; ++i)m_array[i] = a.m_array[i];
  48. }
  49. template<typename T>
  50. T &MyArray<T>::operator [](int index)
  51. {
  52. if (index < 0) index = 0;
  53. if (index >= m_iSize)
  54. {
  55. T *tmp = new T[index + 1];
  56. for (int i = 0; i < m_iSize; ++i) tmp[i] = m_array[i];
  57. for (int i = m_iSize; i <= index; ++i)tmp[i] = T();
  58. delete[] m_array;
  59. m_array = tmp;
  60. m_iSize = index+1;
  61. }
  62. }
  63. template<typename T>
  64. MyArray<T> &MyArray<T>::operator=(const MyArray<T> &a)
  65. {
  66. if (&a != this)
  67. {
  68. delete[] m_array;
  69. m_iSize = a.m_iSize;
  70. m_array = new T[m_iSize];
  71. for (int i = 0; i < m_iSize; ++i) m_array[i] = a.m_array[i];
  72. }
  73. }
  74.  
  75. int _tmain(int argc, _TCHAR* argv[])
  76. {
  77.  
  78. std::cout << minimum(2,4 ) << std::endl;
  79. std::cout << minimum(2.6,4.6 ) << std::endl;
  80. std::cout << minimum<double>(2.5, 4) << std::endl;
  81. std::cout << minimum('c', 'b') << std::endl;
  82. std::cout << minimum("csasaalla", "bella") << std::endl;
  83.  
  84.  
  85. MyArray<int> a;
  86. std::cout << a.GetSize() << std::endl;
  87. a[5]
  88.  
  89.  
  90.  
  91.  
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement