如何将 SWIG 用于 STL 地图?
How to Use SWIG for STL Maps?
我在 ruby 中使用 SWIG。我们可以使用 SWIG 为 Map 使用
生成包装器
%include <std_map.i>
%template(IntMap) std::map<int,int>;
这在接口文件中。
用法示例
2.2.1 :001 > a = Example::IntMap.new
=> std::map,std::allocator< std::pair< int const,int > > > {}
2.2.1 :002 > a[1] = 2
=> 2
2.2.1 :003 > a[6] = -12
=> -12
2.2.1 :004 > a[4] = 92
=> 92
2.2.1 :005 > a
=> std::map,std::allocator< std::pair< int const,int > > > {1=>2,4=>92,6=>-12}
有没有办法直接从 ruby 定义数据类型,以便
键的类型和关联的值是从 ruby 定义的?
例如
2.2.1 :002 > b["asr"] = 2
=> 2
2.2.1 :003 > b["tee"] = -12
=> -12
2.2.1 :004 > b["wetwe"] = 92
在界面文件中使用这个
%include <std_map.i>
namespace std {
%template(Imap) map<swig::GC_VALUE, swig::GC_VALUE>;
}
我在 ruby 中使用 SWIG。我们可以使用 SWIG 为 Map 使用
生成包装器%include <std_map.i>
%template(IntMap) std::map<int,int>;
这在接口文件中。 用法示例
2.2.1 :001 > a = Example::IntMap.new
=> std::map,std::allocator< std::pair< int const,int > > > {}
2.2.1 :002 > a[1] = 2
=> 2
2.2.1 :003 > a[6] = -12
=> -12
2.2.1 :004 > a[4] = 92
=> 92
2.2.1 :005 > a
=> std::map,std::allocator< std::pair< int const,int > > > {1=>2,4=>92,6=>-12}
有没有办法直接从 ruby 定义数据类型,以便 键的类型和关联的值是从 ruby 定义的? 例如
2.2.1 :002 > b["asr"] = 2
=> 2
2.2.1 :003 > b["tee"] = -12
=> -12
2.2.1 :004 > b["wetwe"] = 92
在界面文件中使用这个
%include <std_map.i>
namespace std {
%template(Imap) map<swig::GC_VALUE, swig::GC_VALUE>;
}