覆盖CHAR(或std ::字符串)与痛饮阵列位置? [英] Overwrite char (or std::string) array positions with SWIG?

查看:162
本文介绍了覆盖CHAR(或std ::字符串)与痛饮阵列位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够用C / C ++ void函数,并包装到Python / numpy的与痛饮(INT * I​​NPLACE_ARRAY1,诠释DIM1),一个接收为int *载体为参数,做一些数学上这个载体,并覆盖在同一矢量的结果,而这个结果是可以Python的对象中。就像如下:

I was able to write a void function in C/C++, and wrap to Python/Numpy with SWIG (int* INPLACE_ARRAY1, int DIM1), that receives a int* vector as parameter, do some math on this vector, and overwrite the results on the same vector, and this result was available inside Python's object. Like follows:

    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,诠释DIM1)(DATA_TYPE * INPLACE_ARRAY2,诠释DIM1,DIM2 INT),并运行良好。

This was a test to learn how to wrap pointers to int and double arrays with SWIG using the typemaps (DATA_TYPE* INPLACE_ARRAY1, int DIM1) and (DATA_TYPE* INPLACE_ARRAY2, int DIM1, int DIM2), and worked well.

但问题是,我试过用char /串numpy的载体(如矢量相同的想法 = VEC1 numpy.array(['一','一','一'] ) numpy.array(['一','一','一'],DTYPE = STR),并改变每个位置像(['b','b','b']),但是Python节目的方法'vector_char2D'类型的参数1 '字符*'。这是可能的做同样的字符/字符串?

But the problem is, I've tried the same idea with char/string Numpy vectors (like a vector vec1 = numpy.array(['a','a','a']) or numpy.array(['a','a','a'],dtype=str), and change each position to be like (['b','b','b']), but Python shows in method 'vector_char2D', argument 1 of type 'char *'. It's possible to do the same with 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非常litthe经验。这是可能的的char * 的char ** 和/或的std ::这样做字符串* /的std ::字符串** ?在此先感谢!

I have very litthe experience with SWIG. It's possible to do this with char*, char** and/or std::string*/std::string** ? Thanks in advance!

推荐答案

使用std ::向量:

Use std::vector:

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

这清楚地表明,载体可被修饰,并在其内的字符串可以被修改,并且为STL的载体和字串的SWIG typemaps将很好地工作。注意双,而不是字符串单引号; Python中只有串字符没有这样也没关系。您也可以把它用char *等工作,但它很少是值得的,方便很多上面使用。如果你不想更改信号源,可以包括上述作为通过内嵌%指令在.i文件的包装。

which indicates clearly that the vector can be modified, and strings within it can be modified, and the SWIG typemaps for STL vectors and strings will work nicely. Note the double rather than single quote for string; Python only has strings no chars so it doesn't matter. You can also make it work with char* etc but it is rarely worth the effort, lot easier to use above. If you don't want to change the source, you can include the above as a wrapper in the .i file via an %inline directive.

请注意,你不应该需要外部C预选赛。你应该使用的#include&LT;字符串方式&gt; 不STRING.H

Note you should not need the extern C qualifier. And you should use the #include <string> not string.h.

这篇关于覆盖CHAR(或std ::字符串)与痛饮阵列位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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