Mutate passed 2d vector later

1
2
3
4
5
6
Comparator(const vector<vector<int>>& arr) : mat(arr){};

     bool yo(const mp& p1, const mp& p2) const {
        //mat[2][2]=4;
        return mat[p1.first][p1.second] > mat[p2.first][p2.second];
     }


mat(arr) actually creates a copy of the 2d arr vector but I want to use the same vector passed in the constructor in any operations of the object. You see mat[2][2] doesn't make change to the original 2d vector.
Pointer arithmetic on 2d vector was buggy. Not able to find a solution.


Last edited on
Make Comparator::mat a reference. Why do you need to alter the sort criterion, though? That's a code smell to me.

Pointer arithmetic on 2d vector was buggy.
Well, yeah! Don't do that! If you want to do pointer arithmetic just construct a single linear vector of size width*height.
I don't want to alter the sort criterion. I'm facing a TLE error on the very last few test cases. Hence I wanted to see if I can minimize the time of the prog by eliminating time spent in copying the 2d vector values.

And sorry i didn't get Make Comparator::mat a reference ?
Last edited on
1
2
3
class Comparator {
    public: 
     vector<vector<int>> &mat;
Wow, TLE error vanished. Thanks :)
Topic archived. No new replies allowed.