2

ハイパーキューブクラス、つまり多次元ベクトルを実装しようとしています。一般化に問題があります。三次元超立方体用に作ることはできますが、前述のように、問題はそれを一般化することです。誰か助けてもらえますか?hypercube<4> w(5)合計で5*5 * 5*5要素である各ベクトルの4つの次元と5つの要素を取得するように書くことができるはずです。

これが私が3次元バージョンのために持っているコードです:

#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));
  }
};

ご覧のとおり、再帰は 1 次元の基本ケースまで続きます。また、ベクトルを再帰的に初期化して、内側の次元をずっと渡す必要があります。

そして今、本当のクラス、周りの素敵なインターフェース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 に答える