Exercise · Containers and Iterators · lesson 11 of 11
Exercise: transpose a matrix
A matrix is stored by rows: m[i] is row i, and m[i][j] is the number in row i and column j. Write a function std::vector<std::vector<int>> transpose(const std::vector<std::vector<int>>& m) that returns the transposed matrix. In it, the number m[i][j] sits in row j and column i.
transpose({{1, 2, 3}})→{{1}, {2}, {3}}transpose({{1, 2}, {3, 4}})→{{1, 3}, {2, 4}}transpose({})→{}
Constraints: the matrix is rectangular, with all rows of the same length and none of them empty. An empty matrix has no rows, and its result is empty too.
In the tests, Matrix is a short name for std::vector<std::vector<int>>, set by the line using Matrix = ….
Do not add extra #include lines unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.