Hypercube with multidimensional vectors

后端 未结 2 1450
星月不相逢
星月不相逢 2021-01-15 14:20

I\'m trying to implement a hypercubeclass, that is, multidimensional vectors. I have a problem generalizing it. I\'m able to make one for a three dimensional hypercube, but

2条回答
  •  自闭症患者
    2021-01-15 15:02

    I would suggest something along those lines:

    template  class HQ {
      std::vector > vector;
      public:
        HQ(unsigned size) : vector(size,HQ(size)) {}
    };
    
    template  class HQ {
      std::vector vector;
      public:
        HQ(unsigned size) : vector(size,T()) {}
    };
    
    template  class HQ {};
    

    You can then implement your accessors for the first both templates as you wish. You can also make things a bit more simple and robust by allowing zero-dimensional matrices:

    template  class HQ {
      std::vector > vector;
      public:
        HQ(unsigned size) : vector(size,HQ(size)) {}
    };
    
    template  class HQ {
      T data;
      public:
        HQ(unsigned size) : data() {}
    };
    

    I imagine an access operator would look something like this:

    template  HQ& HQ::operator[](unsigned i) {
      return vector[i];
    }
    template  HQ const& HQ::operator[](unsigned i) const {
      return vector[i];
    }
    

    such that you can write

    HQ hq(5);
    hq[1][4][2][0] = 77;
    

提交回复
热议问题