Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. cc
  2.  
  3. class InvalidIndex : public std::invalid_argument{
  4. public:
  5.     InvalidIndex(std::string c) : std::invalid_argument("Invalid index: "+c), c_(c){  }
  6. private:
  7.     std::string c_;
  8.  
  9. };
  10.  
  11.  
  12. class Mat : public std::vector<double *>{
  13. public:
  14.  
  15.     ~Mat(){
  16.         for(auto p : wec){
  17.             delete [] p.second;
  18.         }
  19.     }
  20.  
  21.     double* addRow(int size){
  22.         auto tab = new double[size];
  23.  
  24.         std::pair<size_t, double*> element(size, tab);
  25.         wec.emplace_back(element);
  26.  
  27.         return tab;
  28.     }
  29.  
  30.     void deleteRow(int r){
  31.         if(r> wec.size()){
  32.             throw InvalidIndex("za duzy indeks");
  33.         }
  34.         delete [] wec.at(r).second;
  35.         wec.erase(wec.begin()+r);
  36.     }
  37.  
  38.     double& operator()(int row, int col){
  39.         if(row> wec.size()){
  40.             throw InvalidIndex("za duzy indeks wiersza");
  41.         }
  42.         if(col> wec.at(row).first){
  43.             throw InvalidIndex("za duzy indeks kolumny");
  44.         }
  45.         return *wec.at(row).second;
  46.     }
  47.  
  48.  
  49.     // dość skomplikowane i nie do końca działa
  50.  
  51.     Mat operator+(const Mat& other){
  52.         size_t size, double_size;
  53.         if(this->wec.size() > other.wec.size()){
  54.             size = this->wec.size();
  55.         } else{
  56.             size = other.wec.size();
  57.         }
  58.         std::vector<std::pair<size_t, double*>> result;
  59.         for(int i=0; i<size; i++){
  60.             if(wec[i].first > other.wec[i].first){
  61.                 double_size = wec[i].first;
  62.             } else{
  63.                 double_size = other.wec[i].first;
  64.             }
  65.             auto d = new double[double_size];
  66.             for(int j=0; j<double_size; j++){
  67.                 d[j] = wec.at(i).second[j]+other.wec.at(i).second[j];
  68.             }
  69.             for(auto p : wec){
  70.                 delete [] p.second;
  71.             }
  72.  
  73.             wec = result;
  74.             return *this;
  75.         }
  76.     }
  77.  
  78. private:
  79.     std::vector<std::pair<size_t, double*>> wec;
  80. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement