So, for homework, one of the things I was tasked with was replacing two rows or two columns with each other using a matrix that was built in an object of type class, using these 3 parameters to determine:
size_t _R;
size_t _C;
std::vector<T> mat;
For example, if I had 3 rows and 3 columns, and an int vector array of 1,2,3,4,5,6,7,8,9, then replacing rows 0 and 1 would show that it looks 4,5 6.1.2.3.7.8.9.
So swap replacement is not a problem here, but I don’t understand what it is , how are you going to do this with O (1) complexity? What I wanted to do is individually switch between each type in a row / column, but then it will be O (n), right? Because it will depend on the number of elements in each row / column.
EDIT: Sample code for what I tried:
void swap_rows(const size_t& r1, const size_t& r2) {
for (size_t i = 0; i < _C; i++)
{
T temp = mat[i + (r1 * _C)];
mat[i + (r1 * _C)] = mat[i + (r2 * _C)];
mat[i + (r2 * _C)] = temp;
}
}
But I believe O (n) is complexity, so no-go: p
Mrguy source
share