Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
9ecbdff9
Commit
9ecbdff9
authored
May 20, 2018
by
scoder
Committed by
GitHub
May 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2282 from gabrieldemarmiesse/example_cpp_basic_tutorial
Added a C++ example in the cython tutorial.
parents
42cc0ef6
db47de21
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
0 deletions
+53
-0
docs/examples/tutorial/primes/primes_cpp.pyx
docs/examples/tutorial/primes/primes_cpp.pyx
+21
-0
docs/src/tutorial/cython_tutorial.rst
docs/src/tutorial/cython_tutorial.rst
+32
-0
No files found.
docs/examples/tutorial/primes/primes_cpp.pyx
0 → 100644
View file @
9ecbdff9
# distutils: language=c++
from
libcpp.vector
cimport
vector
def
primes
(
int
nb_primes
):
cdef
int
n
,
i
cdef
vector
[
int
]
p
p
.
reserve
(
nb_primes
)
# allocate memory for 'nb_primes' elements.
n
=
2
while
p
.
size
()
<
nb_primes
:
# size() for vectors is similar to len()
for
i
in
p
:
if
n
%
i
==
0
:
break
else
:
p
.
push_back
(
n
)
# push_back is similar to append()
n
+=
1
# Vectors are automatically converted to Python
# lists when converted to Python objects.
return
p
docs/src/tutorial/cython_tutorial.rst
View file @
9ecbdff9
...
...
@@ -333,6 +333,38 @@ everywhere. Adding types makes your code less readable, so use them with
moderation.
Primes with C++
===============
With Cython, it is also possible to take advantage of the C++ language, notably,
part of the C++ standard library is directly importable from Cython code.
Let's see what our :file:`primes.pyx` becomes when
using `vector <http://en.cppreference.com/w/cpp/container/vector>`_ from the C++
standard library.
.. note::
Vector in C++ is a data structure which implements a list or stack based
on a resizeable C array. It is similar to the Python ``array``
type in the ``array`` standard library module.
There is a method `reserve` available which will avoid copies if you know in advance
how many elements you are going to put in the vector. For more details
see `this page from cppreference <http://en.cppreference.com/w/cpp/container/vector>`_.
.. literalinclude:: ../../examples/tutorial/primes/primes_cpp.pyx
:linenos:
The first line is a compiler directive. It tells Cython to compile your code to C++.
This will enable the use of C++ language features and the C++ standard library.
Note that it isn't possible to compile Cython code to C++ with `pyximport`. You
should use a :file:`setup.py` or a notebook to run this example.
You can see that the API of a vector is similar to the API of a Python list,
and can sometimes be used as a drop-in replacement in Cython.
For more details about using C++ with Cython, see :ref:`wrapping-cplusplus`.
Language Details
================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment