使用 Swig 将 std::set 转换为 ruby [英] Convert std::set to ruby with Swig

查看:24
本文介绍了使用 Swig 将 std::set 转换为 ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swig 在 ruby​​ 中使用 C++,目前我已经完成了文件 david.h 的一个简单示例

I am using Swig to use C++ in ruby and Currently I have done a simple example of file david.h

#include <stdio.h>
class David
{
public:
    David(int x)
    {
        this->x = x;
    }
    void announce()
    {
        printf("David %d\n", x);
    }
    int x;
};

还有一个像这样swig的文件

And another file for swig like this

%module "david"
%{
#include <libdavid.h>
%}
class David
{
public:
    David(int x);
    void announce();
    int x;
};

我的 extconf.rb 看起来像这样

My extconf.rb looks like this

require 'mkmf'
system('swig -c++ -ruby libdavid.i') or abort
create_makefile('david')

这有助于我在 ruby​​ 中执行一个非常简单的例子

This helps me perform an extremely simple example in ruby like this

2.2.1 :001 > a=David::David.new(42)
 => #<David::David:0x000000022c3ec0 @__swigtype__="_p_David"> 
2.2.1 :002 > a.x
 => 42 

现在我已经非常努力地尝试了一段时间,但我根本无法弄清楚如何使用 c++ stl 中的 set,如示例 此处.如果有人能帮助我解决如何创建一组 stl 整数或向量,并展示如何在该集合/向量上插入和擦除方法,那就太好了.简单的代码示例不仅对我有用,而且对许多将来可能会遇到这种情况的人都非常有用.

Now I have been trying very hard for a while now but I simply can't figure out how to use set from c++ stl as given in example here. It would be really nice if someone could help me out with how I could create a stl set of integers or maybe vector and show how to insert and erase methods work on that set/vector. Simple code samples would be very useful not just for me but for many people who could face this in future.

这只是个人要求,如果您很忙,请随意跳过.
老实说,我使用堆栈溢出已经有一段时间了,但慢慢地我开始对社区感到失望.我一直在发布一些问题,但没有得到满意的答案,没有赞成票,没有反对票,没有,我只是没有收到任何答案.令我惊讶的是,可以在简单的 google 搜索中回答的极其琐碎的问题通常非常受欢迎,而新用户提出的重要问题要么被严重否决,要么完全被忽略.我明白社区不欠我任何东西.但如果有人能解释可能的原因,我将不胜感激.

This just a personal request feel free to skip this if you are busy.
To be honest I have been using stack overflow for quite a while now but slowly I am starting to get disappointed with the community. I have been posting a few questions and I have not received satisfactory answers, No up-votes, no Down-votes, nothing, I just don't receive any answers. What surprises me is that extremely trivial questions that can be answered in a simple google search are often very popular, and important questions from new users are either heavily down voted or totally ignored. I understand that the community does not OWE me anything. But I would be very thankful if someone could explain the possible reasons for that.

谢谢

推荐答案

这并不完整,但可以作为有用的参考.我找不到合适的文档,但它工作正常.将此添加到 swig 文件

This is not complete but could serve as a useful reference. I could not find a proper documentation for this but it works fine. Add this to swig file

%include <std_set.i>
namespace std {
   %template(IntSet) set<int>;
}

现在运行 extconf.rb现在 ruby​​ 支持以下 set 命令

Now run the extconf.rb And now the following commands for set are supported in ruby

2.2.1 :001 > a=David::IntSet.new
 => std::set<int,std::less< int >,std::allocator< int > > [] 
2.2.1 :002 > a.push(4)
 => 4 
2.2.1 :003 > a.push(1)
 => 1 
2.2.1 :004 > a.push(123)
 => 123 
2.2.1 :005 > a.push(612)
 => 612 
2.2.1 :006 > a
 => std::set<int,std::less< int >,std::allocator< int > > [1,4,123,612] 
2.2.1 :007 > a[0]
 => 1 
2.2.1 :008 > a.erase(a.begin)
 => nil 
2.2.1 :009 > a
 => std::set<int,std::less< int >,std::allocator< int > > [4,123,612] 
2.2.1 :010 > a[0]
 => 4 
2.2.1 :011 > a[1]
 => 123 
2.2.1 :012 > a.erase(a.begin)
 => nil 
2.2.1 :013 > a
 => std::set<int,std::less< int >,std::allocator< int > > [123,612] 

push 是如何像 insert for set 那样工作的,我仍然不清楚,但它确实有效.但是这个例子清楚地展示了如何使用 set .类似的方法可用于矢量.

How push works like insert for set is still not clear to me but it works. But this example clearly shows how set can be used. A similar approach can be used for vector.

这个答案当然不完整,非常欢迎有经验的用户提供答案/评论.

This answer is certainly not complete and answers/comments from experienced users are most welcome.

这篇关于使用 Swig 将 std::set 转换为 ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆