在Python中我們可以使用Jupyter Notebook
直接看到結果,例如:
l = [1,2]
l
直接輸出:
[1,2]
那當使用C++的時候,例如:
map<string, int> mp{
{
"one", 1},
{
"two", 2},
{
"three", 3},
{
"four", 4}
};
如果要輸出,就得循環遍歷,可否直接輸出結果呢?
so easy!!! Jupyter Notebook
可以解決一切問題,哈哈~
在github上有一個倉庫,如下所示:
https://github.com/QuantStack/xeus-cling
xeus-cling
是一個用於C++的Jupyter內核,基於C++解釋器和Jupyter協議xeus的原生實現。
目前,支持Mac與Linux,但不支持Windows。
安裝也是非常簡單,首先安裝好Anaconda,在裡面創建一個虛擬環境:
conda create -n cling
切換進去:
conda activate cling
給新環境安裝jupyter
和notebook
conda install jupyter notebook
使用conda-forge
安裝xeus-cling
conda install xeus-cling -c conda-forge
為了加速安裝,請記得給Anaconda配置源!
檢查是否安裝好了內核(kernel):
jupyter kernelspec list
輸出:
python3 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/python3
xcpp11 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp11
xcpp14 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp14
xcpp17 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp17
打開Jupyter Notebook
,就可以看到看到kernel了。
啟動Jupyter Notebook
:
jupyter-notebook
只需要安裝c kernel即可!
可以直接在當前環境中創建c kernel,也可以新開一個環境安裝,下面是在當前環境中直接安裝。
pip install jupyter-c-kernel
install_c_kernel
jupyter kernelspec list
此時,就輸出:
c /home/light/anaconda3/envs/cling/share/jupyter/kernels/c
python3 /home/light/anaconda3/envs/cling/share/jupyter/kernels/python3
xcpp11 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp11
xcpp14 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp14
xcpp17 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp17
啟動Jupyter Notebook
:
jupyter-notebook