Octave中的结构体是所表示的多个字段及其值之间的map。标准模板库map类,该对从std::string和一个octaveCell变量
一个演示oct文件中结构体使用的简单示例是
#include <octave/oct.h>
#include <octave/ov-struct.h>
DEFUN_DLD (structdemo, args, , "Struct Demo")
{
if (args.length () != 2)
print_usage ();
if (! args(0).isstruct ())
error ("structdemo: ARG1 must be a struct");
octave_scalar_map arg0 = args(0).scalar_map_value ();
//octave_map arg0 = args(0).map_value ();
if (! args(1).is_string ())
error ("structdemo: ARG2 must be a character string");
std::string arg1 = args(1).string_value ();
octave_value tmp = arg0.contents (arg1);
//octave_value tmp = arg0.contents (arg1)(0);
if (! tmp.is_defined ())
error ("structdemo: struct does not have a field named '%s'\n",
arg1.c_str ());
octave_scalar_map st;
st.assign ("selected", tmp);
return octave_value (st);
}
它的使用示例是
x.a = 1; x.b = "test"; x.c = [1, 2]; structdemo (x, "b") ⇒ selected = test
上面的示例具体使用octave_scalar_map用于表示单个结构体的类。对于结构体数组octave_map类。注释的代码显示了如何修改演示以处理结构体数组。在这种情况下contents方法返回saCell其可以具有多于一个的元件。因此,为了获得好处octave_value在单结构体示例中,我们将编写
octave_value tmp = arg0.contents (arg1)(0);
其中拖尾(0)是吗()上的运算符Cell对象如果这是一个包含多个元素的真正结构体数组,我们可以使用()运算符
结构体是一个相对复杂的数据容器,在octmap.h这使得使用它们进行编码比仅仅依靠它们更容易contents.
版权所有 © 2024-2025 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2