What is the fastest way to transpose a matrix in C++?

I have a matrix (relatively big) that I need to transpose. For example assume that my matrix is a b c d e f g h i j k l m n o p q r I want the result be as follows: a g m b h n c I o d j p e k ...