【问题标题】:Overwrite char (or std::string) array positions with SWIG?用 SWIG 覆盖 char(或 std::string)数组位置?
【发布时间】:2013-11-16 22:18:33
【问题描述】:

我能够在 C/C++ 中编写一个 void 函数,并使用 SWIG (int* INPLACE_ARRAY1, int DIM1) 包装到 Python/Numpy,它接收 int* vector 作为参数,对此向量进行一些数学运算,并覆盖结果相同的向量,并且此结果在 Python 的对象中可用。如下:

    extern "C" void soma_aloc(int* vetor, int tamanho)
    {
       int m = 0;
       int* ponteiro = new int[tamanho];

       for(m = 0; m < tamanho; m++)
       {           
          ponteiro[m] = vetor[m];
       };

       for(m = 0; m < tamanho; m++)
       {
          ponteiro[m] = ponteiro[m] * 10;
       };

       for(m = 0; m < tamanho; m++)
       {
          vetor[m] = ponteiro[m];
       };

       delete [] ponteiro; 
       };

这是一个学习如何使用 typemaps (DATA_TYPE* INPLACE_ARRAY1, int DIM1)(DATA_TYPE* INPLACE_ARRAY2, int DIM1, int DIM2) 使用 SWIG 包装指向 int 和 double 数组的指针的测试,并且运行良好。

但问题是,我已经尝试使用 char/string Numpy 向量(如向量 vec1 = numpy.array(['a','a','a'])numpy.array(['a','a','a'],dtype=str),并将每个位置更改为 (['b','b','b']),但 Python 显示 in method 'vector_char2D', argument 1 of type 'char *' . char/string 可以做同样的事情吗?

    .cpp:

    extern "C" void vetor_char2D(char* vetorchar, int tamanho_vetor)
    {
       for(int i = 0; i < tamanho_vetor; i++)
       {
           vetorchar[i] = 'b';
       };
    };

    .i:

    %module testestring

    %include stl.i
    %include std_string.i

    %{

  #include <stdio.h>
  #include <stdlib.h>
  //#include <string.h>
  #include <string>
  #include <iostream>

  #define SWIG_FILE_WITH_INIT
      #include "testestring.hpp"

    %}

    %include "numpy.i"

    %init %{
     import_array();
    %}

    %apply (char* INPLACE_ARRAY1, int DIM1) {(char* vetorchar, int tamanho_vetor)}
    %include "testestring.hpp" (just the header of the above function vetor_char2D)
    %clear (char* vetorchar, int tamanho_vetor);

我在 SWIG 方面的经验非常丰富。可以用char*char** 和/或std::string*/std::string** 做到这一点吗?提前致谢!

【问题讨论】:

    标签: python arrays numpy char swig


    【解决方案1】:

    使用 std::vector:

    void vetor_char2D(std::vector<std::string>& vetorchar)
    {
       for (int i = 0; i < vetorchar.size(); i++)
           vetorchar[i] = "b";
    };
    

    这清楚地表明可以修改向量,并且可以修改其中的字符串,并且 STL 向量和字符串的 SWIG 类型映射可以很好地工作。注意字符串的双引号而不是单引号; Python只有字符串没有字符,所以没关系。你也可以让它与 char* 等一起工作,但它很少值得付出努力,在上面更容易使用。如果您不想更改源,可以通过 %inline 指令将上述内容作为包装器包含在 .i 文件中。

    请注意,您不应该需要 extern C 限定符。你应该使用#include &lt;string&gt; 而不是string.h。

    【讨论】:

    • 谢谢哥们!现在它正在工作!我在 .i 文件中使用这个模板: %template(IntVector) std::vector; %template(DoubleVector) std::vector; %template(StringVector) std::vector<:string>; %template(ConstCharVector) std::vector;在 Python 中,我使用(模块名称).StringVector(长度)构建正确的对象。问题:我需要在 Python 中释放这些向量吗?
    • @eldiegon 不,因为 swig 复制了两层之间的字符串,所以没有要管理的内存。
    • @eldiegon 太好了。请注意,感谢人们的方式是投票给您认为有用的答案和 cmets,并标记 最有用/正确/有帮助的答案(复选标记)。
    猜你喜欢
    • 2022-06-14
    • 2011-10-26
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2012-09-03
    • 2022-11-04
    • 2011-01-07
    相关资源
    最近更新 更多