2

我正在尝试实现一个超立方体类,即多维向量。我有一个概括它的问题。我可以为三维超立方体制作一个,但如前所述,问题在于对其进行概括。有人可以帮我吗?您应该能够编写hypercube<4> w(5)以获得每个向量中的 4 个维度和 5 个元素,总共 5*5*5*5 个元素。

这是我的三维版本的代码:

#include <vector>
using std::vector;

using namespace std;

template <int b> 
class Hypercube {
public:

Hypercube(int a) : intvec(a){
    for (int i = 0; i<a;i++) {
        intvec[i].resize(a);
        for (int j = 0;j<a;j++) {
            intvec[i][j].resize(a);
        }
    }
}
vector<vector<int> >& operator[](int i) {
    return intvec[i];
 }

vector<vector<vector<int> > > intvec;
};
4

2 回答 2

2

为此,您需要递归继承来提供正确的向量类型和初始化函数。两者都以递归方式工作,为此我创建了一个名为的小助手结构hcube_info

// hypercube.h
#include <vector>

template<unsigned N>
struct hcube_info;

template<>
struct hcube_info<1>
{ // base version
  typedef std::vector<int> type;
  static type init(unsigned innerdim, int value = 0){
      return type(innerdim, value);
  }
};

template<unsigned N>
struct hcube_info
{ // recursive definition, N dimensions
private:
  typedef hcube_info<N-1> base;
  typedef typename base::type btype;

public:
  typedef std::vector<btype> type;
  static type init(unsigned innerdim, int value = 0){
      return type(innerdim, base::init(innerdim, value));
  }
};

如您所见,一直递归到一维基本情况。我们还需要递归地初始化向量以一直传递内部维度。

现在是真正的类,一个很好的界面hcube_info

template<unsigned N>
struct hypercube
{
private:
  typedef hcube_info<N> info;
  typedef typename info::type vec_type;

public:
  typedef typename vec_type::value_type value_type;
  typedef typename vec_type::size_type size_type;

  explicit hypercube(unsigned innerdim, unsigned value = 0)
    : c(info::init(innerdim, value))
  {
  }

  value_type& operator[](unsigned i){
    return c[i];
  }

  size_type size() const{ return c.size(); }

private:
  vec_type c;
};

测试程序:

#include "hypercube.h"
#include <iostream>

int main(){
  hypercube<4> c(5);
  unsigned s = c.size() * // dim 1
               c[0].size() * // dim 2
               c[0][0].size() * // dim 3
               c[0][0][0].size(); // dim 4
  std::cout << s << '\n'; // outputs: 625 -> 5 * 5 * 5 * 5 -> 5^4
}
于 2011-12-20T22:10:01.923 回答
0

我会建议一些类似的东西:

template <typename T, unsigned dim> class HQ {
  std::vector<HQ<T,(dim-1)> > vector;
  public:
    HQ(unsigned size) : vector(size,HQ<T,(dim-1)>(size)) {}
};

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

template <typename T> class HQ<T,0> {};

然后,您可以根据需要为前两个模板实现访问器。您还可以通过允许零维矩阵使事情变得更加简单和健壮:

template <typename T, unsigned dim> class HQ {
  std::vector<HQ<T,(dim-1)> > vector;
  public:
    HQ(unsigned size) : vector(size,HQ<T,(dim-1)>(size)) {}
};

template <typename T> class HQ<T,0> {
  T data;
  public:
    HQ(unsigned size) : data() {}
};

我想访问运算符看起来像这样:

template <typename T, unsigned dim> HQ<T,(dim-1)>& HQ<T,dim>::operator[](unsigned i) {
  return vector[i];
}
template <typename T, unsigned dim> HQ<T,(dim-1)> const& HQ<T,dim>::operator[](unsigned i) const {
  return vector[i];
}

这样你就可以写

HQ<int,4> hq(5);
hq[1][4][2][0] = 77;
于 2011-12-20T22:01:35.737 回答