Advertisement
nyzinyzi

Untitled

Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<class Key, class Value>
  6. class Element {
  7. public:
  8. Key key;
  9. Value value;
  10. };
  11.  
  12. template<class Key, class Value>
  13. class map {
  14. //friend class Element<Key, Value>;
  15. Element<Key, Value>* tab;
  16. int size; //rozmiar tablicy
  17.  
  18. public:
  19. map() {
  20. tab = NULL;
  21. size = 0;
  22. }
  23. ~map() {
  24. if(tab != NULL){
  25. delete[] tab;
  26. tab = NULL;
  27. }
  28. size = 0;
  29. }
  30. int Size(void) {
  31. return this->size;
  32. }
  33. bool empty(void){
  34. if(size == 0)
  35. return true;
  36.  
  37. return false;
  38. }
  39. map& clear(void){
  40. if(this->tab != NULL){
  41. delete[] this->tab;
  42. tab = NULL;
  43. }
  44. this->size = 0;
  45. return *this;
  46. }
  47. Value & operator [] (const Key &k) {
  48. if(this->size == 0){
  49. tab = new Element<Key, Value>[1];
  50. tab[0].key = k;
  51. size++;
  52. return tab[0].value;
  53. }
  54. else {
  55. for(int i = 0; i < this->size; i++) {
  56. if(this->tab[i].key == k) {
  57. return this->tab[i].value;
  58. }
  59. }
  60. Element<Key, Value>* tmp;
  61. tmp = new Element<Key, Value>[size];
  62. for(int i=0; i<this->size; i++){
  63. tmp[i].value = tab[i].value;
  64. tmp[i].key = tab[i].key;
  65. }
  66. delete[] tab;
  67. size += 1;
  68. tab = new Element<Key, Value>[size];
  69. for(int i=0; i<size-1; i++){
  70. tab[i] = tmp[i];
  71. }
  72. delete[] tmp;
  73. tab[size-1].key = k;
  74. return tab[size-1].value;
  75. }
  76.  
  77. }
  78.  
  79. class iterator{
  80. //friend class Element<Key, Value>;
  81. friend class map;
  82. Element<Key,Value>* pointer;
  83. public:
  84. bool operator != (const iterator& o1){
  85. if(this->pointer != o1.pointer) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. iterator& operator ++ (void){
  91. pointer++;
  92. return *this;
  93. }
  94. Element<Key, Value>* operator -> (void){
  95. return pointer;
  96. }
  97. };
  98. iterator begin(void){
  99. iterator it;
  100. it.pointer = &tab[0];
  101. return it;
  102. }
  103. iterator end(void){
  104. iterator it;
  105. it.pointer = &tab[size];
  106. return it;
  107. }
  108. };
  109. int main(){
  110. map<string, string> m;
  111. m["aa"] = "bb";
  112. m["cc"] = "dd";
  113. string s1;
  114. s1 = m["cc"];
  115.  
  116. for (map<string,string>::iterator i = m.begin(); i != m.end(); ++i)
  117. cout << i->key << " " << i->value << endl;
  118. m.clear();
  119. for (map<string,string>::iterator i = m.begin(); i != m.end(); ++i)
  120. cout << i->key << " " << i->value << endl;
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement