Advertisement
Guest User

swappppppp

a guest
Apr 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. template <typename T>
  10. class MyVector {
  11. private:
  12.     vector <T> arr;
  13.     double Radius;
  14.     int Size_ = arr.size();
  15.  
  16.  
  17. public:
  18.     MyVector(double r, T v0) {Radius=r; arr.push_back(v0);}
  19.     //MyVector(){Radius=50; arr[0]=0;}
  20.     int Size (){ return Size_;}
  21.     void Clear (){arr.clear(); Size_=0;}
  22.     //void Swap(MyVector v){arr.swap(v.arr);}
  23.     T operator[](int i){ return arr[i];}
  24.  
  25.     template <typename T2>
  26.     friend void Swap (MyVector<T2> v1, MyVector<T2> v2);
  27.  
  28.     template <typename T1>
  29.     friend ostream& operator<<(ostream& os, MyVector<T1>& v);
  30.  
  31.     friend double random();
  32.  
  33.     void Start();
  34. };
  35.  
  36. template <typename T2>
  37. void Swap (MyVector<T2> v1, MyVector<T2> v2){
  38.     v1.arr.swap(v2.arr);
  39. }
  40.  
  41. double random(){
  42.     double m=RAND_MAX;
  43.     return 10*rand()/m;}
  44.  
  45. template <typename T>
  46. void MyVector<T>::Start()
  47. { int i=0;
  48.         while (abs(arr[i]-arr[0])<Radius)
  49.         {
  50.             i++;
  51.             arr.push_back(arr[i-1] + random());
  52.         }
  53.     Size_ = arr.size();
  54. };
  55.  
  56. template <typename T1>
  57. ostream& operator<<(ostream& os, MyVector<T1>& v)
  58. {for (int it = 0 ; it < v.arr.size(); it++)
  59.     { os << v.arr[it] << " ";}
  60.     os<<endl;
  61.     return os;
  62. }
  63.  
  64. int main (){
  65.     srand(time(0));
  66.     MyVector<int> v1 (100, 1);
  67.     MyVector<int> v2(100,0);
  68.     v1.Start();
  69.     v2.Start();
  70.     cout<<v1.Size()<<" "<<v2.Size()<<endl;
  71.     cout<<v1<<v2;
  72.     Swap(v2, v1);
  73.     cout<<v1<<v2;
  74.     v1.Clear();
  75.     v2.Clear();
  76.     cout<<v1.Size()<<" "<<v2.Size()<<endl;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement