02-boost库使用
1. boost库下载
官方地址:https://www.boost.org/,下载当前最新版本1.73.0。我们在VS2019上使用boost,下载boost_1_73_0.zip。
2. 安装配置
下载后,解压到某目录,例如:D:\Program Files\boost_1_73_0。
开始菜单,选择Visual Studio 2019 —> x86_x64 Cross Tools Command Prompt for VS 2019,打开命令行,切换到boost库目录:cd /d D:\Program Files\boost_1_73_0
cd /d D:\Program Files\boost_1_73_0
bootstrap.bat # 执行
执行完后再执行如下命令:
b2 install stage --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
注意:
- MSVC 版本号对应,vs2019 可以支持boost c++ library 针对msvc-14.1的编译选项
等待安装按成。
最后记录两个路径:Boost库的包含目录和库目录:
# include path
D:\Program Files\boost_1_73_0
# lib path
D:\Program Files\boost_1_73_0\stage\lib
3. VS2019使用boost
新建vs2019 c++项目,并添加boost依赖库,项目——>属性——>选择VC++目录,在包含目录 和 库目录添加以上2个路径,如下图:
或可以新建一个系统环境变量BOOST_DIR,值为D:\Program Files\boost_1_73_0。
-
在path系统环境变量中添加:%BOOST_DIR%;
-
VC++包含目录中添加: $(BOOST_DIR);
-
VC++库目录中添加: $(BOOST_DIR)\stage\lib。
代码测试
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace std;
using namespace boost;
int main()
{
//system("chcp 65001");
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;
}
运行后输出(正常)如下:
4. boost分类
4.1 按照功能分类
4.1.1 字符串和文本处理
a) Conversion
b) Format
c) IOStream
d) Lexical Cast
e) Regex
f) Spirit
g) String Algo
h) Tokenizer
i) Wave
j) Xpressive
4.1.2 容器
a) Array
b) Bimap
c) Circular Buffer
d) Disjoint Sets
e) Dynamic Bitset
f) GIL
g) Graph
h) ICL
i) Intrusive
j) Multi-Array
k) Multi-Index
l) Pointer Container
m) Property Map
n) Property Tree
o) Unordered
p) Variant
4.1.3 迭代器
a) GIL
b) Graph
c) Iterators
d) Operators
e) Tokenizer
4.1.4 算法
a) Foreach
b) GIL
c) Graph
d) Min-Max
e) Range
f) String Algo
g) Utility
4.1.5 函数对象和高阶编程
a) Bind
b) Function
c) Functional
d) Functional/Factory
e) Functional/Forward
f) Functional/Hash
g) Lambda
h) Member Function
i) Ref
j) Result Of
k) Signals
l) Signals2
m) Utility
4.1.6 泛型编程
a) Call Traits
b) Concept Check
c) Enable If
d) Function Types
e) GIL
f) In Place Factory, Typed In Place Factory
g) Operators
h) Property Map
i) Static Assert
j) Type Traits
4.1.7 模板元编程
a) Function Types
b) Fusion
c) MPL
d) Proto
e) Static Assert
f) Type Traits
4.1.8 预处理元编程
a) Preprocessors
4.1.9 并发编程
a) Asio
b) Interprocess
c) MPI
d) Thread
4.1.10 数学和数字
a) Accumulators
b) Integer
c) Interval
d) Math
e) Math Common Factor
f) Math Octonion
g) Math Quaternion
h) Math/Special Functions
i) Math/Statistical Distributions
j) Multi-Array
k) Numeric Conversion
l) Operators
m) Random
n) Rational
o) uBLAS
4.1.11 排错和测试
a) Concept Check
b) Static Assert
c) Test
4.1.12 数据结构
a) Any
b) Bitmap
c) Compressed Pair
d) Fusion
e) ICL
f) Multi-Index
g) Pointer Container
h) Property Tree
i) Tuple
j) Uuid
k) Variant
4.1.13 图像处理
a) GIL
4.1.14 输入输出
a) Asio
b) Assign
c) Format
d) IO State Savers
e) IOStreams
f) Program Options
g) Serialization
4.1.15 跨语言混合编程
a) Python
4.1.16 内存管理
a) Pool
b) Smart Ptr
c) Utility
4.1.17 解析
a) Spirit
4.1.8 编程接口
a) Function
b) Parameter
4.1.19 杂项
a) Compressed Pair
b) Conversion
c) CRC
d) Date Time
e) Exception
f) Filesystem
g) Flyweight
h) Lexical Cast
i) Meta State Machine
j) Numeric Conversion
k) Optional
l) Polygon
m) Program Options
n) Scope Exit
o) Statechart
p) Swap
q) System
r) Timer
s) Tribool
t) Typeof
u) Units
v) Utility
w) Value Initialized
4.1.20 编译器问题的变通方案
a) Compatibility
b) Config
4.2 常用库
- Regex:正则表达式库
- Spirit:LL parser framework,用C++代码直接表达EBNF
- Graph:图组件和算法
- Lambda:在调用的地方定义短小匿名的函数对象,很实用的functional功能
- concept check:检查泛型编程中的concept
- Mpl:用模板实现的元编程框架
- Thread:可移植的C++多线程库
- Python:把C++类和函数映射到Python之中
- Pool:内存池管理
- smart_ptr:5个智能指针