• <legend id='R6YHt'><style id='R6YHt'><dir id='R6YHt'><q id='R6YHt'></q></dir></style></legend>

    <i id='R6YHt'><tr id='R6YHt'><dt id='R6YHt'><q id='R6YHt'><span id='R6YHt'><b id='R6YHt'><form id='R6YHt'><ins id='R6YHt'></ins><ul id='R6YHt'></ul><sub id='R6YHt'></sub></form><legend id='R6YHt'></legend><bdo id='R6YHt'><pre id='R6YHt'><center id='R6YHt'></center></pre></bdo></b><th id='R6YHt'></th></span></q></dt></tr></i><div id='R6YHt'><tfoot id='R6YHt'></tfoot><dl id='R6YHt'><fieldset id='R6YHt'></fieldset></dl></div>

      <small id='R6YHt'></small><noframes id='R6YHt'>

      <tfoot id='R6YHt'></tfoot>
        <bdo id='R6YHt'></bdo><ul id='R6YHt'></ul>

      1. Cython &amp;C++:通过引用传递

        Cython amp; C++: passing by reference(Cython amp;C++:通过引用传递)

      2. <i id='shy2N'><tr id='shy2N'><dt id='shy2N'><q id='shy2N'><span id='shy2N'><b id='shy2N'><form id='shy2N'><ins id='shy2N'></ins><ul id='shy2N'></ul><sub id='shy2N'></sub></form><legend id='shy2N'></legend><bdo id='shy2N'><pre id='shy2N'><center id='shy2N'></center></pre></bdo></b><th id='shy2N'></th></span></q></dt></tr></i><div id='shy2N'><tfoot id='shy2N'></tfoot><dl id='shy2N'><fieldset id='shy2N'></fieldset></dl></div>

            <tbody id='shy2N'></tbody>

            <legend id='shy2N'><style id='shy2N'><dir id='shy2N'><q id='shy2N'></q></dir></style></legend>
              <bdo id='shy2N'></bdo><ul id='shy2N'></ul>

              <small id='shy2N'></small><noframes id='shy2N'>

              1. <tfoot id='shy2N'></tfoot>

                  本文介绍了Cython &amp;C++:通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Cython 和 C++ 的菜鸟,所以我有一个关于参数传递的问题.我想避免在以下场景中传递参数的副本:

                  I am a noob with Cython and C++, so I have a question on argument passing. I want to avoid passing a copy of an argument in the following scenario:

                  # somefile.pyx
                  #distutils: language = c++
                  from libcpp.vector cimport vector
                  
                  def add_one(vector[int] vect):
                      cdef int i
                      n = vect.size()
                      for i in range(n):
                          vect[i] += 1
                  
                  cdef vector[int] v
                  for i in range(100000):
                      v.push_back(i)
                  add_one(v) # <-- ??
                  

                  我希望方法 add_one 只是就地"修改 v.我相信在 C++ 中,您可以通过在参数前面加上 & 来实现这一点,这意味着对指针的任何更改都会传递给被指点者.这样,您就不必担心传递指针或实际对象,即

                  I want the method add_one to just modify v "in-place." I believe in C++, you can achieve this by pre-pending the argument with &, which means that any changes to the pointer is passed to the pointee. That way, you don't have to worry about passing a pointer or the actual object, i.e.

                  add_one(v); # in c++
                  

                  我可以在 Cython 中做同样的事情,还是必须明确地将 arg 类型更改为引用,即 def add_one(vector[int]* vect)?

                  Can I do the same in Cython, or do I have to explicitly change the arg type to a reference instead, i.e. def add_one(vector[int]* vect)?

                  推荐答案

                  找到了我自己问题的答案.显然,您可以通过引用传递,但该函数必须是 cdef'ed,而不是 def'ed.即

                  Found the answer to my own question. Apparently, you can pass by reference, but the function MUST be cdef'ed, not def'ed. i.e.

                  # somefile.pyx
                  #distutils: language = c++
                  from libcpp.vector cimport vector
                  
                  cdef void add_one(vector[int]& vect):
                      cdef int i
                      n = vect.size()
                      for i in range(<int>n):
                          vect[i] += 1
                  
                  cdef vector[int] v
                  for i in range(100000):
                      v.push_back(i)
                  add_one(v)
                  

                  这篇关于Cython &amp;C++:通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  <tfoot id='z0eE9'></tfoot>
                    <tbody id='z0eE9'></tbody>

                    <small id='z0eE9'></small><noframes id='z0eE9'>

                      <legend id='z0eE9'><style id='z0eE9'><dir id='z0eE9'><q id='z0eE9'></q></dir></style></legend>

                          <bdo id='z0eE9'></bdo><ul id='z0eE9'></ul>

                        • <i id='z0eE9'><tr id='z0eE9'><dt id='z0eE9'><q id='z0eE9'><span id='z0eE9'><b id='z0eE9'><form id='z0eE9'><ins id='z0eE9'></ins><ul id='z0eE9'></ul><sub id='z0eE9'></sub></form><legend id='z0eE9'></legend><bdo id='z0eE9'><pre id='z0eE9'><center id='z0eE9'></center></pre></bdo></b><th id='z0eE9'></th></span></q></dt></tr></i><div id='z0eE9'><tfoot id='z0eE9'></tfoot><dl id='z0eE9'><fieldset id='z0eE9'></fieldset></dl></div>