我如何将一组整数集变成C 中的3D矢量

How can I turn a set of set of integers into 3D vectors in C++?

本文关键字:中的 矢量 3D 整数 一组      更新时间:2023-10-16

我在data[12][51]阵列中具有一个数据矩阵,该矩阵是12 x 51矩阵。

如果我有 set<set<int>> rows,例如

{(1,2,5),(3,7,8),(9,11)}将表示数据矩阵的行索引。

我想形成一组行索引的矩阵,因此(1,2,5(的元素将是由第1,第二和第5行组成的3 x 51矩阵。

vector<double> features[12];
for (int i = 0; i < 12; i++){
    for (int j = 0; j < 51; j++){
        features[i].push_back(data[i][j]);
    }
}   

因此,在上面的部分中,features[i]将返回数据矩阵的整个i'。并给定任意尺寸的set<set<int>> rows

vector<vector<vector<double>>> sets;
for (auto & it : rows){
    vector<vector<double>> temp;
    for (int i : it){
        temp.push_back(features[i]);
    }
    sets.push_back(temp);
}

但是,当我尝试打印矩阵的这种向量时,

for (int i = 0; i < sets.size(); i++){
    for (int j = 0; j < sets[0].size(); j++){
        for (int z = 0; z < sets[0][0].size(); z++){
            cout << sets[i][j][z] << " , ";
        }
        cout << endl;
    }
    cout << " === Matrix === " << endl;
}

可执行的冻结,只是停止。

因此,如果以上方法不应该工作,那么我如何将一组整数变成3D向量?

在一个人的工具箱中动态尺寸的矩阵总是很高兴的可以随时将其弹出。我敢肯定那里有成千上万的地方。

根据需要添加浇头,并将全面的文件保持在安全的位置。

您可以添加的东西:获取subsatrix,加法等

#include <vector>
#include <algorithm>
template <typename T>
class Matrix
{
private:
  typedef std::vector<std::vector<T>> _Mat;
  _Mat matrix_;
public:
  Matrix(int rows, int cols)
  {
     matrix_.resize(rows);
     for (auto& v : matrix_)
     {
       v.resize(cols);
     } 
  }
  Matrix(int rows, int cols, const T* data)
  {
     matrix_.resize(rows);
     for (auto& v : matrix_)
     {
       v = std::vector<T>(data, data + cols);
       data += cols;
     } 
  }
  size_t rowCount() const  
  { 
    return matrix_.size(); 
  }
  size_t colCount() const  
  { 
    return (rowCount()) ? matrix_[0].size() : 0; 
  }
  const std::vector<T>& getRow(int n) const
  {
    return matrix_.at(n);
  }
  void setRow(int row, const std::vector<T>& v)
  {
    if (row >= rowCount() || v.size() != colCount())
    {
      throw std::exception(); // choose your favorite exception class
    }
    matrix_.at(row) = v;
  }
  std::vector<T> getCol(int col)
  {
    std::vector<T> result;
    std::for_each(matrix_.begin(), matrix_.end(), 
      [&](std::vector<T>& r)
      {
        result.push_back(r.at(col));
      });
    return result;
  }
  void setCol(size_t col, const std::vector<T>& v)
  {
    if (col >= colCount() || v.size() != rowCount())
    {
      throw std::exception(); // choose your favorite exception class
    }
    for (size_t i = 0; i < matrix_.size(); ++i)
    {
      matrix_[i][col] = v[i];
    }
  }
  std::vector<T>& operator[](size_t row) 
  { 
    return matrix_.at(row); 
  }
  const std::vector<T>& operator[](size_t row) const 
  { 
    return matrix_.at(row);
  }
};

int main()
{
   Matrix<double> a(12, 51);
   Matrix<double> b(3, 51);
   Matrix<double> c(12, 2);
   b.setRow(0, a.getRow(1));
   b.setRow(1, a.getRow(25));
   b.setRow(2, a.getRow(12));
   Matrix<double> c(12, 2);
   c.setCol(0, a.getRow(3));
   c.setCol(1, a.getCol(4));  // <<  throws!!
   std::vector<Matrix<double>> matrices;
   matrices.push_back(a);
   matrices.push_back(b);
   matrices.push_back(c);
   Matrix<std::vector<double>> matrixOfVectors;
   return 0;
}

为什么不使用这样的样式:

int nMatrix[rowSize][volumnSize][deepSize] = { 0 };

实际上,我们通常使用数组操作3D数据