【问题标题】:Blitz++ array in struct结构中的 Blitz++ 数组
【发布时间】:2016-11-16 04:25:48
【问题描述】:

我想要一个包含多个 blitz++ 数组的结构。该程序创建了这样一个结构,但是我无法正确分配对象。将带有 pointers 的结构制定为在结构外部分配的 blitz++ 数组的唯一替代方法是什么?

#include <iostream>
#include <blitz/array.h>

using namespace std;
using namespace blitz;

struct Bstruct{
    Array<double,1> B;
};

int main(){

    Bstruct str;
    Array<double,1> x(10);
    x = 1.0;
    str.B = x;

    cout << "x = " << x << endl;
    cout << "str.B = " << str.B << endl;

    return 0;
}

➜  blitz_struct git:(master) ✗ ./struct
x = (0,9)
[ 1 1 1 1 1 1 1 1 1 1 ] 

str.B = (0,-1)
[ ]

【问题讨论】:

  • 上面截取的代码末尾显示了程序的输出。 xstr.B 应该是一样的。
  • 大概blitz::Array 没有提供正确/深拷贝赋值运算符。而且它明显的主页已经关闭,所以我无法检查标题!
  • 是的,我知道主页已关闭,这真的很痛苦。无论如何,这暂时有效。

标签: c++ struct blitz++


【解决方案1】:

我发现这行得通:

#include <iostream>
#include <blitz/array.h>

using namespace std;
using namespace blitz;

struct Bstruct{
    Array<double,1> B;
};

int main(){

    Bstruct str;
    Array<double,1> x(10);
    x = 1.0;
    str.B.resize(10);
    str.B = 1.0;

    cout << "x = " << x << endl;
    cout << "str.B = " << str.B << endl;

    return 0;
}

➜  blitz_struct git:(master) ✗ ./struct                       
x = (0,9)
[ 1 1 1 1 1 1 1 1 1 1 ]

str.B = (0,9)
[ 1 1 1 1 1 1 1 1 1 1 ]

【讨论】:

  • 对,所以blitz::Array 似乎没有提供完整的复制分配运算符。如果我需要非标准容器,我一定会避免使用它。你有什么理由不能使用std::vector,它确实具有合理的复制分配语义?
  • 是的,不幸的是,这是一段用 blitz++ 编写的遗留代码,将其更改为其他任何东西都需要大量的工作。这就是为什么。
  • 哦,对不起,也许我没有问到你的问题。你在问为什么我不能使用std::vector 而不是结构?我想为元素命名而不是container.at(3),例如container.named_element
  • @FlorianOswald 我的意思是阵列本身。嗯,祝维护顺利!
  • 告诉我吧! :-( 无论如何感谢您的收听!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多