Q:

Reordering of matrix in c++

I have a matrix in C++, A(n,n) and a vector P(n) which looks something like this:
  P = [ 3 6 1 13 12 16 ... ] 

it contains numbers 1:n but not in an ascending order but scrambled. 
My aim is to change the rows and columns of matrix A to the same order.  For example since P[0] = 3 I want the 3rd row and 3rd column to move to the 1st row and column in matrix A.
But because the matrix could be potentially really large, I can't use another matrix of size same as A because that would be wasteful. 
In matlab this can be done simply by using the command:
 A(P,P);

Any ideas on how to do the same thing in c++? 

A:

I will suggest using a level of indirection, to locate each matrix in a cell. 
Let's say your matrix object is called M. Instead of using
M[R][C]

to refer to the cell in row R, column C (assuming row-major matrix ordering), you will have an associated pair of vectors, let's call them y and x, so the value of the cell in row R column C is:
M[y[R]][x[C]]

Initially, both y and x vectors map each "logical" row or column to the corresponding physical row and column, that is both y and x contain [0..max_row] and [0..max_col]. 
Then, to effect the swapping in your question, you simply copy your P vector to the y and x vectors. 
You should implement your matrix not directly, as a two-dimensional  std::vector, but as a standalone class:
class Matrix {

public:

// ...

    auto operator()(size_t R, size_t C) const;

    auto &operator()(size_t R, size_t C);

// ...
};

and implement the indirect mapping of rows and columns as part of the class implementation.