G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 10th, 2020 169 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #pragma once
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class Vector {
  8. private:
  9.     size_t dimensions;
  10.     T* data;
  11. public:
  12.     Vector<T>(size_t dimensions) :
  13.         dimensions(dimensions), data(new T[dimensions] ()) {}
  14.     virtual ~Vector<T>() {
  15.         if (data) delete[] data;
  16.     }
  17.  
  18.     size_t size() const {
  19.         return dimensions;
  20.     }
  21.  
  22.     const T& at(size_t index) const {
  23.         return data[index];
  24.     }
  25.  
  26.     void delete_data() {
  27.         if (data) delete[] data;
  28.     }
  29.     T & operator[](size_t index) { return data[index]; }
  30.  
  31.     Vector<T> operator=(Vector<T> &vector) {
  32.         data = new T[vector.size()];
  33.         dimensions = vector.size();
  34.         for (size_t i = 0; i < vector.size(); i++) {
  35.             data[i] = vector[i];
  36.         }
  37.         vector.delete_data();
  38.         return *this;
  39.     }
  40.     /*Vector<T>(const Vector<T>& copied) {
  41.         dimensions = copied.dimensions();
  42.         this = new Vector<T>(dimensions);
  43.  
  44.         for (size_t i = 0; i < dimensions; i++) {
  45.             data[i] = copied[i];
  46.         }
  47.     }*/
  48.     friend T& operator+(const Vector<T>& a, const Vector<T>& b);
  49.     friend bool operator==(const Vector<T>& a, const Vector<T>& b);
  50.     friend bool operator!=(const Vector<T>& a, const Vector<T>& b);
  51. };
  52.  
  53.                 /*===============================
  54.                 ==========Operatory=============
  55.                 ===============================*/
  56. template <typename T>
  57. T & operator+(const Vector<T>& a, const Vector<T>& b) {
  58.     Vector<T> c(a.size());
  59.     for (size_t i = 0; i < a.size(); i++) {
  60.         c[i] = a[i] + b[i];
  61.     }
  62.     return c;
  63. }
  64.  
  65. template <typename T>
  66. bool operator==(const Vector<T>& a, const Vector<T>& b) {
  67.     if (&a == &b) return true;
  68.     if (a.size() != b.size())
  69.         return false;
  70.     for (size_t i = 0; i < a.size(); i++) {
  71.         if (a[i] != b[i]) return false;
  72.     }
  73.     return true;
  74. }
  75.  
  76. template <typename T>
  77. bool operator!=(const Vector<T>& a, const Vector<T>& b) {
  78.     if (&a != &b) return true;
  79.     if (a.size() != b.size())
  80.         return true;
  81.     for (size_t i = 0; i < a.size(); i++) {
  82.         if (a[i] == b[i]) return false;
  83.     }
  84.     return true;
  85. }
  86.  
  87. template <typename T>
  88. ostream & operator<< (ostream &result, Vector<T> &out) {
  89.     for (size_t i = 0; i < out.size(); i++) {
  90.         result << out[i] << endl;
  91.     }
  92.     return result;
  93. }
  94. /*===============================
  95. ==========Funkcje================
  96. =================================*/
  97. template<typename T>
  98. Vector<T> add(const Vector<T>& a, const Vector<T>&b) {
  99.     Vector<T> c(a.size());
  100.     for (size_t i = 0; i < a.size(); i++) {
  101.         c[i] = a[i] + b[i];
  102.     }
  103.     return c;
  104. }
  105.  
  106. template<typename T>
  107. bool equals(const Vector<T>& a, const Vector<T>& b) {
  108.     if (&a == &b) return true;
  109.     if (a.size() != b.size())
  110.         return false;
  111.     for (size_t i = 0; i < a.size(); i++) {
  112.         if (a[i] != b[i]) return false;
  113.     }
  114.     return true;
  115. }
  116. int main()
  117. {
  118.     Vector<int> v1(10);
  119.  
  120.     for (size_t i = 0; i < 10; i++) {
  121.         v1[i] = i+1;
  122.     }
  123.     cout << v1;
  124.  
  125.     Vector<int> v2(11);
  126.     v2 = v1;
  127.  
  128.     //cout << v2;
  129. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top