gpt4 book ai didi

java - SWIG C++ 列表到 Java

转载 作者:太空狗 更新时间:2023-10-29 23:00:35 25 4
gpt4 key购买 nike

我正在尝试使用 SWIG 来包装 C++,它定义了一个像这样的对象列表

    typedef std::list<Country> CountryList;

为此,我必须在接口(interface)文件中包含什么?

谢谢,

jack

最佳答案

只需将以下内容添加到您的 swig 接口(interface)文件中:

%include "std_list.i"
%include "Country.h" /* or declaration of Country */
%template(Country_List) std::list<Country>;

编辑:我不知道 swig 不提供 std_list.i用于 Java 包装 std::list .查看为 std::vector 提供的那个, 这可以适应 std::list .请注意,大多数功能不可用,因为您真的想使用迭代器来访问列表元素。无论如何,这里是:

标准列表.i

%include <std_common.i>

%{
#include <list>
#include <stdexcept>
%}

namespace std {
template<class T> class list {
public:
typedef size_t size_type;
typedef T value_type;
typedef const value_type& const_reference;
list();
list(size_type n);
size_type size() const;
%rename(isEmpty) empty;
bool empty() const;
void clear();
const_reference back();
%rename(add) push_back;
void push_back(const value_type& x);
void pop_back();
};
}

而不是换行 std::list<Country> ,您可能希望将其转换为 Java 中的相应列表。但是,为此,您需要编写适当的 typemaps .

关于java - SWIG C++ 列表到 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com