超立方体多维向量 [英] Hypercube with multidimensional vectors

查看:152
本文介绍了超立方体多维向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个hypercubeclass,就是多维向量。
我有一个问题概括它。我能够作一项所述的三维超立方体,但如前所述,问题概括它。谁能帮助我?你应该能够编写超立方体4;> W(5)来得到4尺寸和是5 * 5 * 5 * 5在总元件每个向量5元素

下面是code我对三维版本:

 的#include<矢量>
使用std :: vector的;使用命名空间std;模板< INT B>
超立方体类{
上市:超立方体(int类型的):INTVEC(一){
    的for(int i = 0; I< A;我++){
        INTVEC [I] .resize(一);
        对于(INT J = 0; J< A; J ++){
            INTVEC [I] [J] .resize(一);
        }
    }
}
矢量<矢量< INT> >&安培;运算符[](int i)以{
    返回INTVEC [I]
 }矢量<矢量<矢量< INT> > > INTVEC;
};


解决方案

对于这个工作,你需要递归的传承提供正确的载体类型和初始化函数。这两个工作递归,为此,我创建了一个名为小帮手结构 hcube_info

  // hypercube.h
#包括LT&;矢量>模板<无符号N'GT;
结构hcube_info;模板<>
结构hcube_info&所述1为卤素;
{//基础版本
  的typedef的std ::矢量<&INT GT;类型;
  静态类型的init(无符号innerdim,int值= 0){
      返回类型(innerdim,值);
  }
};模板<无符号N'GT;
结构hcube_info
{//递归定义,N维
私人的:
  typedef的hcube_info< N-1>基础;
  的typedef typename的基地::类型BTYPE;上市:
  的typedef的std ::矢量<&BTYPE GT;类型;
  静态类型的init(无符号innerdim,int值= 0){
      返回类型(innerdim,基地::初始化(innerdim,值));
  }
};

正如你所看到的,递归一路一维的基本情况。我们还需要递归初始化向量一路过关斩将通过内部尺寸。

和现在真正的类,绕了一个漂亮的界面的 hcube_info

 模板<无符号N'GT;
超立方体结构
{
私人的:
  typedef的hcube_info< N>信息;
  的typedef typename的信息::类型vec_type;上市:
  的typedef typename的vec_type :: value_type的value_type的;
  的typedef typename的vec_type :: size_type的SIZE_TYPE;  明确超立方体(无符号innerdim,无符号值= 0)
    C(信息::初始化(innerdim,值))
  {
  }  VALUE_TYPE&安培;运算符[](无符号I){
    返回C [I];
  }  SIZE_TYPE大小()const的{返回c.size(); }私人的:
  vec_type℃;
};

测试程序:

 的#includehypercube.h
#包括LT&;&iostream的GT;诠释主(){
  超立方体&所述; 4为H. C(5);
  无符号的S = c.size()* //暗淡1
               C [0] .size()* //暗淡2
               C [0] [0] .size()* // 3暗淡
               C [0] [0] [0] .size(); // 4暗淡
  性病::法院LT&;<小号所述&;&下;的'\\ n'; //输出:625 - > 5 * 5 * 5 * 5 - > 5 ^ 4
}

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 as mentioned, the problem is generalizing it. Could anyone help me? You should be able to write hypercube<4> w(5) to get 4 dimensions and 5 elements in each vector that is 5*5*5*5 elements in total.

Here is the code I have for the three dimensional version:

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

解决方案

For this to work, you need recursive inheritence to provide the correct vector type and the initialization function. Both work recursively, for which I created a little helper struct called 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));
  }
};

As you can see, recursion all the way to the one dimensional base case. We also need to recursively initialize the vector to pass the inner dimension all the way through.

And now the real class, a nice interface around the 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;
};

Test program:

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

这篇关于超立方体多维向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆