Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cc
- class InvalidIndex : public std::invalid_argument{
- public:
- InvalidIndex(std::string c) : std::invalid_argument("Invalid index: "+c), c_(c){ }
- private:
- std::string c_;
- };
- class Mat : public std::vector<double *>{
- public:
- ~Mat(){
- for(auto p : wec){
- delete [] p.second;
- }
- }
- double* addRow(int size){
- auto tab = new double[size];
- std::pair<size_t, double*> element(size, tab);
- wec.emplace_back(element);
- return tab;
- }
- void deleteRow(int r){
- if(r> wec.size()){
- throw InvalidIndex("za duzy indeks");
- }
- delete [] wec.at(r).second;
- wec.erase(wec.begin()+r);
- }
- double& operator()(int row, int col){
- if(row> wec.size()){
- throw InvalidIndex("za duzy indeks wiersza");
- }
- if(col> wec.at(row).first){
- throw InvalidIndex("za duzy indeks kolumny");
- }
- return *wec.at(row).second;
- }
- // dość skomplikowane i nie do końca działa
- Mat operator+(const Mat& other){
- size_t size, double_size;
- if(this->wec.size() > other.wec.size()){
- size = this->wec.size();
- } else{
- size = other.wec.size();
- }
- std::vector<std::pair<size_t, double*>> result;
- for(int i=0; i<size; i++){
- if(wec[i].first > other.wec[i].first){
- double_size = wec[i].first;
- } else{
- double_size = other.wec[i].first;
- }
- auto d = new double[double_size];
- for(int j=0; j<double_size; j++){
- d[j] = wec.at(i).second[j]+other.wec.at(i).second[j];
- }
- for(auto p : wec){
- delete [] p.second;
- }
- wec = result;
- return *this;
- }
- }
- private:
- std::vector<std::pair<size_t, double*>> wec;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement