什么是 C++ 中的映射函数

what are mapping functions in c++?

本文关键字:映射函数 C++ 什么      更新时间:2023-10-16

我搜索了n维数组中的映射函数,但没有找到特定的答案。我想知道多维数组是如何工作的 i c++?在 N 维数组中查找特定索引处的元素的一般公式是什么?

给定一个 k 维数组arr[n,1][n,2][n,3]...[n,k],元素在 arr[x,1][x,2][x,3]...[x,k] 处的索引是 x,k + x,(k-1) * n,k + x,(k-2) * n,k * n,(k-1) + ... + x,1 * n,2 * n,3 * ... * n,k

Redefine 运算符 [] 返回带有重新定义的 oprator[] 的对象

template<T>
struct Matrix {
  // initialization and access checking skipped
  typedef std::vector<T> t_raw;
  typedef std::vector<t_raw> t_col;
  t_col m_mat;
  struct Idx {
    Matrix* mat;
    size_t row;
    T& operator[](size_t col) {
      return this->mat->m_mat[ this->row ][col];
    };
  }
  Idx operator[](size_t row) {
    Idx idx;
    idx.mat = this;
    idx.row = row;
    return idx;
  };
  friend class Idx;
};
Matrix<int> m;
m[1][2] = 5;