I am trying to create the transpose of a matrix - by traversing into 2-D vectors and assigning the values of indices accordingly.
[[2, 4, 6], [6, 8, 9]] --> [[2,6], [4, 8], [6, 9]] // transpose matrix representation
Here's my code
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
vector<vector<int>> result = {};
for (size_t i = 0; i < matrix.size(); i )
{
for (size_t j = 0; j < matrix[0].size(); j )
{
int element = matrix[i][j];
result[j][i].push_back(element); // error occurs here
}
}
return result;
}
While calling the function with proper main body - I am getting the error in calling the push_back
member function.
The error message
transpose_matrix.cpp:11:30: error: request for member 'push_back' in '(& result.std::vector<std::vector<int> >::operator[](j))->std::vector<int>::operator[](i
', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
11 | result[j][i].push_back(element);
| ^~~~~~~~~
If you've any alternative suggestion then it'll be also helpful.
CodePudding user response:
result[j][i]
is not a vector
, it's an int
. Also, you have to at least resize the vector
to the number of rows in the transposed matrix (equal to number of columns in the input matrix).
#include <iostream>
#include <vector>
std::vector<std::vector<int>> transpose(std::vector<std::vector<int>>& matrix)
{
std::vector<std::vector<int>> result;
if (matrix.empty())
return result;
result.resize(matrix[0].size());
for (size_t i = 0; i < matrix.size(); i )
{
for (size_t j = 0; j < matrix[0].size(); j )
{
int element = matrix[i][j];
result[j].push_back(element);
}
}
return result;
}
int main()
{
std::vector<std::vector<int>> v{
{1,2},
{2,3},
{3,4}
};
auto res = transpose(v);
for (auto const& row : res)
{
for (auto ele : row)
std::cout << ele << " ";
std::cout << std::endl;
}
}
CodePudding user response:
result[j][i].push_back(element)
is pointing to a integer value in a vector<vector<int>>
, and integers has no method "push_back" that is the reason of the error
you can either:
result[j][i] = element;
to overwrite the element at j,i position (be aware of the size of the vector, so you dont try to write an element out of the boundaries of it) or just
result.push_back(element);
to add a new integer in the vector<vector<int>>
CodePudding user response:
Solved the error
The error is caused due to wrong interpretation of the vector & int - result[j][i] is an integer not a vector
After solving that - there's another problem left with resizing the empty vector list. To avoid that - I ended up with assigning the vector size at the time of declaring it. And that solved the whole problem. - vector<vector<int>> res(matrix[0].size(), vector<int>(matrix.size(), 0));
Further explanation - If main matrix has
m*n
dimension then its transpose must haven*m
dimension
Whole Solution
vector<vector<int>> transpose(vector<vector<int>> &matrix)
{
// Here assigning the empty matrix caused error while runtime
vector<vector<int>> res(matrix[0].size(), vector<int>(matrix.size(), 0));
for (int i = 0; i < matrix.size(); i )
{
for (int j = 0; j < matrix[0].size(); j )
{
// Assigning the elements to transpose matrix
res[j][i] = matrix[i][j];
}
}
return res;
}