1. 介绍
C++ 中 NumCpp 和 Python 中的 numpy 作用类似。
2.1 boost 库安装(NumCpp 库依赖于 boost 库,建议版本不需要太新,也不要太旧)
2.1.1 安装包
Web: https://www.boost.org/users/history/version_1_70_0.html
Version: https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.7z
2.1.2 安装
- 将安装包放在:
F:\Program Files\booat_C++
解压
- 进入:
F:\Program Files\booat_C++\boost_1_70_0
- 打开 VS 2019 点击 “工具”→“命令行”→“开发者 PowerShell§”
- 使用:
**********************************************************************
** Visual Studio 2019 Developer PowerShell v16.7.1
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
PS D:\Computer\C++\Projects\firstDemo> cd 'F:\Program Files\booat_C++\boost_1_70_0'
PS F:\Program Files\booat_C++\boost_1_70_0>
- 运行 " .\bootstrap.bat"(等待一会)
PS F:\Program Files\booat_C++\boost_1_70_0> .\bootstrap.bat
Building Boost.Build engine
Generating Boost.Build configuration in project-config.jam for msvc...
Bootstrapping is done. To build, run:
.\b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
.\b2 --help
- Getting started guide:
http://boost.org/more/getting_started/windows.html
- Boost.Build documentation:
http://www.boost.org/build/
生成了几个文件夹,其中有一个 bjam.exe:
PS F:\Program Files\booat_C++\boost_1_70_0> ls
目录: F:\Program Files\booat_C++\boost_1_70_0
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/11/9 16:09 bin.v2
d----- 2019/4/10 4:02 boost
d----- 2019/4/10 3:42 doc
d----- 2019/4/10 4:02 libs
d----- 2019/4/10 3:35 more
d----- 2020/11/9 16:03 stage
d----- 2019/4/10 3:35 status
d----- 2019/4/10 3:35 tools
-a---- 2020/11/9 16:02 388608 b2.exe
-a---- 2020/11/9 16:02 388608 bjam.exe
-a---- 2019/4/10 3:35 850 boost-build.jam
-a---- 2019/4/10 3:35 989 boost.css
-a---- 2019/4/10 3:35 6308 boost.png
-a---- 2019/4/10 3:35 19167 boostcpp.jam
-a---- 2019/4/10 3:35 2997 bootstrap.bat
-a---- 2020/11/9 16:02 3214 bootstrap.log
-a---- 2019/4/10 3:35 10807 bootstrap.sh
-a---- 2019/4/10 3:35 769 index.htm
-a---- 2019/4/10 4:02 5567 index.html
-a---- 2019/4/10 3:35 291 INSTALL
-a---- 2019/4/10 3:35 11886 Jamroot
-a---- 2019/4/10 3:35 1338 LICENSE_1_0.txt
-a---- 2020/11/9 16:02 154 project-config.jam
-a---- 2019/4/10 3:35 541 README.md
-a---- 2019/4/10 3:35 2608 rst.css
- 最后执行(等待 4 – 5 分钟):
.\bjam.exe --toolset=msvc-14.1 architecture=x86 address-model=64 link=static --build-type=complete --with-system --with-thread --with-date_time --with-filesystem --with-serialization
会生成文件夹
.\stage\lib
- 在 VS 2019 中添加路径
点击 “调试”→“调试属性”
选择 “VC++ 目录”,在 “包含目录” 和 “库目录” 分别添加路径“如图”
F:\Program Files\booat_C++\boost_1_70_0
F:\Program Files\booat_C++\boost_1_70_0\stage\lib
- 代码测试
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace std;
using namespace boost;
int main() {
double a = lexical_cast<double>("3.1415926");
string str = lexical_cast<string>("3.1415926");
cout << "This is a number: " << a << endl;
cout << "This is a string: " << str << endl;
int b = 0;
try {
b = lexical_cast<int>("neo");
}
catch (bad_lexical_cast& e) {
cout << e.what() << endl;
}
return 0;
}
成功
2.2 NumCpp 的下载使用
2.2.1 下载
git clone https://github.com/dpilger26/NumCpp.git
2.2.2 使用
将 NumCpp 文件下的 include 文件中的内容(红圈圈的两个)拷贝出来,放置在项目中(跟 main 函数同一级目录)
2.2.3 测试
#include <cstdio>
#include <iostream>
#include "include\\NumCpp.hpp"
int main() {
nc::NdArray<int> a0 = { {1, 2}, {3, 4} };
return 0;
}
可以通过就可以了