C++ for Pythonistas and Data Scientists

An introduction to Cling to help learn C++

Image taken from https://negativespace.co/ .

When studying mathematics at University I was introduced to both Python and R in a statistics module. Since then I have only stuck to those two languages and only dabbled in other languages when needed.

Recently, I have wanted to improve my programming foundations and learn more about the underlying concepts that we data scientists take for granted with Python and R, while also finding some ways to improve my workflows. And so I undertook the challenge of learning C++, doing this then led me to find Cling .

Cling

Cling is an interactive interpreter for C++ which helps give a similar experience to coding with Python. This has had a huge benefit to my learning experience with C++ and I am sure it can help many others.

Hello, World!

A basic C++ program for Hello, World! can be daunting and off-putting for a Python user. Below is an example:

#include <iostream>
#include <vector>int main() {
 std::cout << “Hello, World!” << std::endl;
 return 0;
}

Once written, the program can then be compiled (if there are no errors) and then ran to see the output.

Below is the same but when using the Cling interpreter:

#include <iostream>
std::cout << “Hello, World”! << std::endl;

No compiling is needed!

This is a very simple example, but using Cling can allow you to explore C++ functions, vectors and more in an interactive way allowing you to experiment more.

For example, to experiment with vectors you can use the interpreter to help learn the functions and syntax.

#include <vector>
vector<int> data;data.push_back(1);
data.push_back(2) 
// return the vector to see what happened
data;

Jupyter Notebooks

There is also a Jupyter Kernel which can make learning even more interactive while also helping you learn ways you can present your work in a data science flow.

Installation

Installing is easy and can be done with Conda :

conda install -c conda-forge cling

To get the Jupyter Kernel also install `xeus-cling`:

conda install xeus-cling -c conda-forge

Setting Up Jupyter Notebooks

Run Jupyter Notebook as normal and then select a C++ kernel as seen in the screenshot below:

Final Remarks

Interpreting C++ is not a final solution and using C++ like this means you lose a lot of the power possible with the language. But, as a learning tool Cling is invaluable and can help speed up your learning process if you already have some foundations in other languages.

The associated code and notebook are available on my GitHub , this also includes code starter code to import a simple single columned CSV.

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章