C++ 交换向量表示的矩阵中的两列

C++ 交换向量表示的矩阵中的两列,c++,matrix,time-complexity,stdvector,C++,Matrix,Time Complexity,Stdvector,我有以下问题: 我有一个表示二维矩阵的向量,我有行数和列数,还有其他一些无关的东西 // A synomon for the type of the grayvalues typedef unsigned int grayvalue_t; static_assert(std::numeric_limits<grayvalue_t>::max()<= std::numeric_limits<size_t>::max(),

我有以下问题: 我有一个表示二维矩阵的向量,我有行数和列数,还有其他一些无关的东西

// A synomon for the type of the grayvalues
typedef unsigned int grayvalue_t;
static_assert(std::numeric_limits<grayvalue_t>::max()<=
              std::numeric_limits<size_t>::max(),
              "grayvalue_t maximum should be smaller than size_t maximum");

// Number of rows
size_t _R;
// Number of columns
size_t _C;
// Maximum grayvalue
grayvalue_t _MAX_G;
// Pixels' grayvalues
std::vector<grayvalue_t> _pixels;
我遗漏了什么吗? 我想得到一些帮助


谢谢

与许多其他CS问题一样,答案是:多了一层间接寻址


一个选项是维护一个映射,该映射将矩阵中的列索引映射到向量中的实际索引。也就是说,矩阵列并不总是按顺序存储,而行的元素将保持连续。映射开始时将0映射到0 1映射到1,以此类推。要交换两列,只需交换它们在映射中的条目。如果您还需要按行遍历整个数组,则需要查阅有关列顺序的映射。

谢谢,经过一些更改后,这似乎很容易。祝您今天过得愉快!
/// swaps between rows r1 and r2
/// Time complexity: O(1)
void swap_rows(const size_t& r1, const size_t& r2) {

}

/// swaps between columns c1 and c2
/// Time complexity: O(1)
void swap_cols(const size_t& c1, const size_t& c2) {

}