Advertisement
Guest User

Untitled

a guest
May 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. /*Maksymalna
  2. pojemność kontenera powinna być podawana każdorazowo w momencie jego konkretyzacji.
  3. Kontener powinien mieć swój iterator (np. taki jak na slajdzie 295, wykł. 10). Następnie
  4. wykorzystaj istnienie nowego operatora dzielenia oraz iteratora kontenera aby napisać
  5. funkcję-algorytm zwracającą wartość średnią ze wszystkich przechowywanych elementów*/
  6.  
  7.  
  8. #include<iostream>
  9. #include<assert.h>
  10.  
  11. template <typename T>
  12. class IntStack {
  13. enum { ssize = 100 };
  14. T stack[ssize];
  15. int top;
  16. public:
  17. IntStack() : top(0) {}
  18. void push(const T& i) {
  19. assert(top < ssize); stack[top++] = i;
  20. }
  21. T pop() {
  22. assert(top > 0); return stack[--top];
  23. }
  24. class iterator {
  25. IntStack& s;
  26. int index;
  27. public:
  28. iterator(IntStack& is) : s(is), index(0) {}
  29. iterator(IntStack& is, bool) : s(is), index(s.top) {}
  30. int operator++() { // Prefix
  31. assert(index < s.top);
  32. return s.stack[++index];
  33. }
  34. int operator++(int) { // Postfix
  35. assert(index < s.top);
  36. return s.stack[index++];
  37. }
  38. bool operator==(const iterator& rv) const {
  39. return index == rv.index;
  40. }
  41. bool operator!=(const iterator& rv) const {
  42. return index != rv.index;
  43. }
  44. };
  45. iterator begin() { return iterator(*this); }
  46. iterator end() { return iterator(*this, true);}
  47. friend class iterator;
  48. };
  49.  
  50. int main(){
  51.  
  52. /*IntStack<int> is;
  53. // zapełniamy stos wartościami
  54. for(int i = 0; i < 20; i++)
  55. is.push(i);
  56. // przemieszczamy się po stosie za pomocą iteratora:
  57. IntStack<int>::iterator it = is.begin();
  58. while(it != is.end()) // nie musimy znać rozmiaru stosu..
  59. printf("%i\n",it++);*/
  60.  
  61. float a=5.4,b=6.5,c=2.3;
  62.  
  63. IntStack<float>wsk;
  64. wsk.push(a);
  65. wsk.push(b);
  66. wsk.push(c);
  67.  
  68. IntStack<float>::iterator cos=wsk.begin();
  69. while(cos!=wsk.end())
  70. printf("%i\n",cos++);
  71.  
  72.  
  73.     system("pause");
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement