julia> using Cxx
julia> cxx""" #include <vector> """
true
julia> cxx""" std::vector<int> a = std::vector<int> (5,6); """
true
julia> icxx""" a[0]; """
(int &) 6
julia> b = icxx""" a; """
(class std::vector<int, class std::allocator<int> >) {
}
julia> b[0]
6
julia> b
(class std::vector<int, class std::allocator<int> >) {
}
Run Code Online (Sandbox Code Playgroud)
当输入Julia终端时,上述代码显示存在矢量数据.但是,我更愿意将其完全转移到Julia数组中.这样做的最佳方法是什么?
注意:最终共享库将返回a std::vector<int>,因此更明确的问题是如何最好地转换std::vector<int>为标准Julia向量.(这是指示b例代码中的变量).
提前致谢.
编辑:为什么有问题的原因似乎并不清楚,所以希望以下将有所帮助(它直接来自上面的代码)
julia> unsafe_wrap(Array, pointer(b), length(b))
ERROR: MethodError: objects of type Ptr{Int32} are not callable
julia> @cxx b;
ERROR: Could …Run Code Online (Sandbox Code Playgroud) 我试图在使用该Array类型的同时保持代码的可重用性,但不确定如何在不允许“非法”类型通过的情况下继续进行。示例功能:
foo = function(x::Array{Number,2})
print(x)
end
foo([1 2 3; 4 5 6; 7 8 9])
# 15
foo([1 2 3])
# MethodError: no method matching (::Array{Int64,1})
# Closest candidate is (::Array{Number,2})
foo(["alpha" "beta"; "pi" "rho"])
# MethodError: no method matching (::Array{String,2})
# Closest candidate is (::Array{Number,2})
Run Code Online (Sandbox Code Playgroud)
然而,第一个例子尽管理论上是有效的促销,但foo([1 2 3; 4 5 6; 7 8 9])还是回来了。我不想为每个函数调用手动转换;我也不想必须将所有数组声明为该类型。我知道我可以将函数调用替换为:MethodError: no method matching (::Array{Int64,2})(::Array{Number,2})Array{Number,2}
foo = function(x::Array)
print(x)
end
Run Code Online (Sandbox Code Playgroud)
但是,这允许将任何维度和类型的数组放入函数中。我能想到的唯一的其他选择是添加样板代码,我最初允许任何和所有数组进入,手动检查它们的类型和大小,然后从那里开始,但这感觉不优雅。
有什么建议么?提前致谢。(注:我使用的是 Julia 0.6.3)