从 Go 中迭代 `std::vector<std::string>`?

戴夫

我正在尝试使用 Go 中的 C++ 库。我对 Go 的经验也很少。

我的 C++ 方法如下所示:

std::vector<std::string> getAttribute(const std::string key);

我的.swigcxx文件中有这个

%include "std_string.i"
%include "std_vector.i"

%include "MyCode.h"

%template(StringVector) std::vector<std::string>;

%{
  #include "MyCode.h"
%}

但是 Go 不想让我访问它。我已经通过以下方式打印了一些信息:

attribs := ipuInfo.GetAttribute("foo")
fmt.Printf("%T\n", attribs)
fmt.Printf("%+v\n", attribs)

我得到:

bar.SwigcptrStringVector
<random number - address perhaps?>

但是我不知道如何获得向量的各个内容。我试过:

for index, val := range bar.GetAttribute("foo") {
}

但这给出了:

cannot range over bar.GetAttribute("foo") (type interface {})

我也试过调用.Capacity()方法,但它没有声明(来自https://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/)。我也试过attribs[0],它也不喜欢那样。

有没有人有任何建议?

非常感谢。

戴夫

看起来是函数重载的问题。我有 2 个getAttribute具有相同名称和不同参数的函数。当我重命名该方法以使其唯一Capacity()Get()工作时(尽管range没有)。

看起来 Swig 试图生成包装器代码来调用正确的方法,但无法处理不同的返回类型

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除std :: vector <std :: string>中的元素

C ++ std :: vector <std :: string>迭代器segfaults

使用std :: find检查std :: vector <std :: vector <std :: string >>中的项目

通过迭代器从std :: vector中删除

将std :: string索引转换为std :: vector中的整数

将std :: vector <std :: vector <std :: string >>转换为std :: vector <std :: vector <double >>

使用 std::views 将 std::vector<string> 转换为 C++20 中的 std::string

std::string 不是 std::vector<char> 吗?

如何从std :: vector <char>构造std :: string?

初始化std :: string的std :: vector

如何在C ++ 17中将std :: string转换为std :: vector <std :: byte>?

std :: vector <string>奇怪的行为

VisualC ++:String ^和std :: vector

删除与另一个给定std :: string中的字符匹配的std :: vector <std :: string>中的元素

C ++中的std :: vector与std :: array

在std :: set中查找std :: vector的元素

在推入std :: vector的过程中删除对象中的std :: string

std :: dynarray与std :: vector

如何在c ++ 11中初始化std :: vector <std :: string>的静态constexpr成员?

为什么std :: vector <std :: string>中的字符串以相同的数据地址结尾?

计算std :: string的已分配内存(以及std :: vector中字符串的使用)

指针转换后处理 std::vector<char> 中的 std::string 时出现段错误

在C ++ 20中按长度排序std :: vector <std :: string>的代码是否正确?

从文件中提取单词并将其放入std :: array <string,4>的`std :: vector`中

基于c ++范围的std :: vector迭代的顺序

迭代时 std::vector 自然线程安全

使用标准C ++同时迭代std :: vector

std :: vector迭代器作为类成员

std :: algorithms友好的方法来迭代`std :: vector <std :: vector <something >>`