首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多维向量的超立方体

具有多维向量的超立方体
EN

Stack Overflow用户
提问于 2011-12-21 01:09:39
回答 2查看 1.7K关注 0票数 2

我正在尝试实现一个超立方体类,也就是多维向量。我在概括它时遇到了问题。我可以为三维超立方体制作一个,但正如前面提到的,问题是推广它。有人能帮帮我吗?您应该能够编写hypercube<4> w(5)来获得4维和每个向量中的5个元素,即总共5*5*5*5个元素。

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

代码语言:javascript
复制
#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;
};
EN

回答 2

Stack Overflow用户

发布于 2011-12-21 06:10:02

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

代码语言:javascript
复制
// 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的一个很好的接口

代码语言:javascript
复制
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;
};

测试程序:

代码语言:javascript
复制
#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
}
票数 2
EN

Stack Overflow用户

发布于 2011-12-21 06:01:36

我会提出一些类似的建议:

代码语言:javascript
复制
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> {};

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

代码语言:javascript
复制
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() {}
};

我想一个访问操作员应该是这样的:

代码语言:javascript
复制
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];
}

这样您就可以编写

代码语言:javascript
复制
HQ<int,4> hq(5);
hq[1][4][2][0] = 77;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8579207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档