Advertisement
llsumitll

erase the element from a vector.

Feb 12th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6. class Shop
  7. {
  8. string name;
  9. public:
  10. Shop() { }
  11. Shop(string n): name(n) { }
  12. void getShopName() {cout << name << endl;}
  13. };
  14.  
  15. class ASubject
  16. {
  17. private:
  18. vector<Shop* >list;
  19. /*
  20. This function is finding the element in the vector
  21. if the element is available in the array it return the position( index number )
  22. of that element.
  23. if element is not available it return -1.
  24. */
  25.  
  26. int get_position(Shop *s)
  27. {
  28. for(unsigned short i = 0; i < list.size() ; i++)
  29. {
  30. if( list[i] == s )
  31. return i;
  32. }
  33. return -1;
  34. }
  35. public:
  36. void attach(Shop *shop)
  37. {
  38. list.push_back(shop);
  39. }
  40.  
  41. /*
  42. for unsubscribe the shop from the notification list.
  43. */
  44. void detach(Shop * shop)
  45. {
  46. int index = get_position(shop);
  47. if (index == -1){
  48. cout << "element is not found in the list " << endl;
  49. }
  50. else
  51. list.erase(list.begin()+index);
  52.  
  53. }
  54. void notify(float price)
  55. {
  56. // todo : notify function is yet to implement.
  57. }
  58.  
  59. void print_vector()
  60. {
  61.  
  62. /* old style of for loop with stl*/
  63. //for(unsigned int i = 0 ; i < pe.size() ; i++)
  64. // cout << pe[i] << " " ;
  65.  
  66. for(Shop *x : list)
  67. cout << x << " " << endl; ;
  68. cout << endl;
  69. }
  70.  
  71.  
  72. };
  73.  
  74. int main()
  75. {
  76. ASubject a ;
  77.  
  78. Shop s1;
  79. Shop s2, s3, s4, s5, s6, s7, s8, s9;
  80. a.attach(&s1); // 0
  81. a.attach(&s2); // 1
  82. a.attach(&s3); // 2
  83. a.attach(&s4); // 3
  84. a.attach(&s5); // 4
  85. a.attach(&s6); // 5
  86. a.attach(&s7); // 6
  87. a.attach(&s8); // 7
  88.  
  89. a.print_vector();
  90.  
  91. a.detach(&s5);
  92. a.print_vector();
  93.  
  94. a.detach(&s4);
  95. a.print_vector();
  96.  
  97. a.detach(&s1);
  98. a.print_vector();
  99.  
  100. a.detach(&s8);
  101. a.print_vector();
  102.  
  103. a.detach(&s9);
  104. a.print_vector();
  105.  
  106.  
  107. return 0;;
  108.  
  109. }
  110. ====================Output====================
  111. sumitmac:RevisionCpp sumit$ ./a.out
  112. 0x7ff7b5aa3858
  113. 0x7ff7b5aa3830
  114. 0x7ff7b5aa3818
  115. 0x7ff7b5aa3800
  116. 0x7ff7b5aa37e8
  117. 0x7ff7b5aa37d0
  118. 0x7ff7b5aa37b8
  119. 0x7ff7b5aa37a0
  120.  
  121. 0x7ff7b5aa3858
  122. 0x7ff7b5aa3830
  123. 0x7ff7b5aa3818
  124. 0x7ff7b5aa3800
  125. 0x7ff7b5aa37d0
  126. 0x7ff7b5aa37b8
  127. 0x7ff7b5aa37a0
  128.  
  129. 0x7ff7b5aa3858
  130. 0x7ff7b5aa3830
  131. 0x7ff7b5aa3818
  132. 0x7ff7b5aa37d0
  133. 0x7ff7b5aa37b8
  134. 0x7ff7b5aa37a0
  135.  
  136. 0x7ff7b5aa3830
  137. 0x7ff7b5aa3818
  138. 0x7ff7b5aa37d0
  139. 0x7ff7b5aa37b8
  140. 0x7ff7b5aa37a0
  141.  
  142. 0x7ff7b5aa3830
  143. 0x7ff7b5aa3818
  144. 0x7ff7b5aa37d0
  145. 0x7ff7b5aa37b8
  146.  
  147. element is not found in the list
  148. 0x7ff7b5aa3830
  149. 0x7ff7b5aa3818
  150. 0x7ff7b5aa37d0
  151. 0x7ff7b5aa37b8
  152.  
  153.  
  154.  
Tags: vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement