0


【C++】开源:Boost库常用组件配置使用

😏★,°:.☆( ̄▽ ̄)/$:.°★ 😏
这篇文章主要介绍Boost库常用组件配置使用。
无专精则不能成,无涉猎则不能通。——梁启超
欢迎来到我的博客,一起学习,共同进步。
喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

😏1. 项目介绍

项目Github地址:

https://github.com/boostorg/boost

Boost库在线书籍:

https://wizardforcel.gitbooks.io/the-boost-cpp-libraries/content/0.html

Boost是一个流行的、开源的C++库集合,提供了各种功能强大的库和工具,扩展了C++语言的能力,并为开发者提供了更高级别的抽象和工具。Boost库经过广泛的使用和测试,被认为是C++社区的事实标准之一。

Boost库包含了多个模块,每个模块都提供了不同领域的功能和工具,覆盖了诸如字符串操作、数据结构、算法、日期时间处理、文件系统、线程、网络、正则表达式等各个方面。以下是一些常用的Boost库:

1.Boost.Asio:提供了异步I/O操作的网络编程库,支持TCP、UDP、串口等网络协议。

2.Boost.Smart_Ptr:提供了智能指针类,如shared_ptr和weak_ptr,用于方便地进行内存管理。

3.Boost.Filesystem:提供了对文件系统的访问和操作,包括文件和目录的创建、删除、遍历等。

4.Boost.Regex:提供了正则表达式的功能,用于进行文本匹配和搜索操作。

5.Boost.Thread:提供了跨平台的多线程编程接口,简化了线程的创建、同步和通信等操作。

6.Boost.Serialization:提供了对象的序列化和反序列化功能,可以将对象以二进制或XML格式进行存储和传输。

除了以上列举的库之外,Boost还包含了许多其他功能丰富的库,如Boost.Math用于数学计算、Boost.Graph用于图论算法、Boost.Test用于单元测试等。Boost库通常以头文件方式提供,使用Boost只需包含相应的头文件,并链接对应的库文件。

Boost库的目标是提供高质量和高可移植性的C++代码,因此它的代码质量很高,并且支持各种主流操作系统和编译器。Boost库的开发是一个开放的社区驱动过程,接受用户的反馈和贡献,并定期发布新版本。

Boost.Thread特性

线程管理:Boost.Thread可以创建、启动、停止和管理线程。它提供了线程对象(boost::thread)来表示一个线程,并提供了一些类似于启动线程、等待线程结束、检查线程状态等方法。

互斥锁和条件变量:Boost.Thread 提供了互斥锁和条件变量等同步原语,用于实现线程之间的互斥和同步。互斥锁可以保护共享资源的访问,条件变量可以实现线程之间的等待和通知机制。

线程间数据共享:Boost.Thread提供了一些线程间数据共享的机制,如原子操作、线程局部存储等,可以保证在多线程环境下的数据访问的正确性和一致性。

线程间通信:Boost.Thread 还提供了一些线程间通信的机制,如消息队列、信号量等,可以实现线程之间的信息传递和同步。

并发算法:Boost.Thread 还提供了一些并发算法,如并行循环(parallel loop)、并行排序(parallel sort)等,可以在多核处理器上有效地执行并行计算任务。

Boost.Serialization特性

序列化:Boost.Serialization 可以将对象序列化为字节流。通过使用 boost::serialization 命名空间中的 << 运算符,您可以将对象写入输出流中。
反序列化:Boost.Serialization 可以从字节流反序列化对象。通过使用 boost::serialization 命名空间中的 >> 运算符,您可以从输入流中读取字节并重建对象。
版本控制:Boost.Serialization 支持版本控制,可以在不同版本之间进行对象的序列化和反序列化。这使得改变对象的结构时可以进行向前和向后兼容。
对象关联:Boost.Serialization 能够正确地处理对象之间的关联关系和引用。当序列化一个对象时,被引用的对象也会被自动序列化,并在反序列化时进行恢复。
自定义扩展:Boost.Serialization 允许开发者对自定义类型进行扩展和适配,以支持序列化和反序列化操作。通过为自定义类型添加 serialize 函数,可以指定如何将对象转换为字节流和从字节流中恢复。

Boost.Math特性

数字运算:Boost.Math 提供了大量的数学函数,例如幂函数、指数函数、对数函数、三角函数、双曲函数等。这些函数支持各种数据类型,包括整数、浮点数和复数,并且具有高精度和高效率。
特殊函数:Boost.Math 实现了许多特殊函数,如伽玛函数、贝塞尔函数、椭圆积分、误差函数和球贝塞尔函数等。这些函数在科学计算、信号处理、概率统计和物理建模等领域中具有广泛的应用。
数值常量:Boost.Math 提供了许多常用的数学常量,如圆周率 π、自然对数底 e、黄金比例 φ 等。这些常量可以直接在代码中使用,而无需手动输入。
概率分布:Boost.Math 实现了各种概率分布函数和随机数生成器,如正态分布、均匀分布、泊松分布和二项分布等。这些函数和生成器可用于模拟实验、数据分析和统计推断等应用场景。
统计算法:Boost.Math 包含一些统计计算的算法,如平均值、标准差、方差、协方差和相关系数等。这些算法可以用于描述和分析数据集的统计特性。
几何计算:Boost.Math 提供了一些用于几何计算的函数和类,如点、向量、矩阵、线段、射线和多边形等。这些工具可以用于解决几何问题,如交点计算、距离计算和形状检测等。

Boost.Time特性

boost::posix_time:提供了对时间点和时间间隔进行操作的类和函数。它支持高精度的时间表示,并提供了各种算术和比较运算符,以及格式化和解析时间的能力。
boost::gregorian:提供了对 Gregorian 阳历日期进行操作的类和函数。它支持日期的算术和比较运算符,以及格式化和解析日期的能力。它还提供了一些有用的函数,如计算某个日期的下一个工作日、计算某个月份的天数等。
boost::date_time:提供了一个更高级的日期和时间处理框架,可以处理多种不同的日历系统、时区和时间精度。它建立在 boost::posix_time 和 boost::gregorian 的基础上,提供了更丰富的功能。例如,它支持多种不同的日历系统,如 Julian 日历、季节日历等;支持多种不同的时区表示和转换;还提供了更复杂的日期和时间算法,如计算某个日期之前或之后的工作日,计算某个日期所在的周是当年的第几周等。

Boost.Geometry几何计算库特性

几何数据模型:Boost.Geometry 定义了一套通用的几何数据模型,包括点、线、多边形等。这个数据模型可以适用于二维和三维空间,并支持不同的几何类型。
几何算法:Boost.Geometry 提供了许多几何算法,包括距离计算、相交检测、包围盒计算、缓冲区计算等。这些算法可以应用于几何对象上,以解决各种几何问题。
几何运算:Boost.Geometry 支持各种几何运算,如交集、并集、差集、对称差集等。这些运算可以用于组合和修改几何对象。
空间索引:Boost.Geometry 提供了一些空间索引数据结构,如 R-tree 和 Quadtree,用于高效地进行空间查询和搜索。
输入/输出支持:Boost.Geometry 支持各种几何数据格式的输入和输出,包括 WKT (Well-Known Text)、WKB (Well-Known Binary) 等。这使得与其他几何库和工具进行数据交换变得更加容易。

😊2. 环境配置

下面进行环境配置:

# apt安装常用模块sudoapt-getinstall libboost-dev
# Boost.Geometry只在boost1.75以上支持wget https://dl.bintray.com/boostorg/release/1.76.0/source/boost_1_76_0.tar.gz
tar-xzvf boost_1_76_0.tar.gz
cd boost_1_76_0
./bootstrap.sh --prefix=/usr/local
sudo ./b2 installsudoaptinstall libboost-all-dev
# 验证高版本安装ls /usr/local/include/boost/geometry/

😆3. 使用说明

下面进行使用分析:

Boost.Thread使用示例

创建线程示例:

#include<iostream>#include<boost/thread.hpp>// 线程函数voidthreadFunction(){// 输出线程相关信息
    std::cout <<"Thread ID: "<< boost::this_thread::get_id()<< std::endl;
    std::cout <<"Hello from a thread!"<< std::endl;}intmain(){// 创建线程并启动
    boost::thread threadObj(threadFunction);// 多个线程类似// 等待线程结束
    threadObj.join();// 输出主线程相关信息
    std::cout <<"Thread ID: "<< boost::this_thread::get_id()<< std::endl;
    std::cout <<"Main thread exiting..."<< std::endl;return0;}

编译运行:

g++ -o main main.cpp -lboost_thread-lpthread
./main
Thread ID: 7f65d8552700
Hello from a thread!
Thread ID: 7f65d8553740
Main thread exiting...

Boost.Serialization使用示例

#include<iostream>#include<fstream>#include<boost/archive/text_oarchive.hpp>#include<boost/archive/text_iarchive.hpp>// 要进行序列化和反序列化的示例类classMyClass{public:int data;double d;
    std::string str;// 声明 Boost 序列化函数为友元函数friendclassboost::serialization::access;// Boost 序列化函数(将对象转换为字节流)template<classArchive>voidserialize(Archive & ar,constunsignedint version){
        ar & data;
        ar & d;
        ar & str;}};intmain(){// 创建一个 MyClass 对象并设置数据
    MyClass obj;
    obj.data =42;
    obj.d =1.005;
    obj.str ="hello";// 将对象序列化到文件
    std::ofstream outputFile("data.txt");
    boost::archive::text_oarchive outputArchive(outputFile);
    outputArchive << obj;
    outputFile.close();// 从文件中反序列化对象
    std::ifstream inputFile("data.txt");
    boost::archive::text_iarchive inputArchive(inputFile);
    MyClass restoredObj;
    inputArchive >> restoredObj;
    inputFile.close();// 输出反序列化后的对象数据
    std::cout <<"Restored data: "<< restoredObj.data << std::endl;
    std::cout <<"Restored d: "<< restoredObj.d << std::endl;
    std::cout <<"Restored str: "<< restoredObj.str << std::endl;return0;}

编译运行:

g++ -o main main.cpp -lboost_serialization&& ./main
Restored data: 42
Restored d: 1.005
Restored str: hello

Boost.Math使用示例

#include<iostream>#include<boost/math/constants/constants.hpp>#include<boost/math/special_functions/bessel.hpp>intmain(){// 计算圆周率double pi = boost::math::constants::pi<double>();
    std::cout <<"Pi: "<< pi << std::endl;// 贝塞尔函数double besselJ0 = boost::math::cyl_bessel_j(0,2.0);
    std::cout <<"Bessel J0(2.0): "<< besselJ0 << std::endl;return0;}

编译运行:

g++ -o main main.cpp -lboost_math_c99-lboost_math_c99f&& ./main
Pi: 3.14159
Bessel J0(2.0): 0.223891

Boost.Time使用示例

#include<iostream>#include<boost/date_time/posix_time/posix_time.hpp>longGetTime();intmain(){// 获取当前系统时间
    boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
    std::cout <<"Current system time: "<< now << std::endl;// 格式化输出当前系统时间
    std::string formattedTime = boost::posix_time::to_simple_string(now);
    std::cout <<"Formatted current system time: "<< formattedTime << std::endl;// 日期增减
    boost::posix_time::ptime tomorrow = now + boost::gregorian::days(1);
    std::cout <<"Tomorrow: "<< tomorrow << std::endl;// 时间增减
    boost::posix_time::ptime nextHour = now + boost::posix_time::hours(1);
    std::cout <<"Next hour: "<< nextHour << std::endl;// 时间差计算
    boost::posix_time::time_duration diff = nextHour - now;
    std::cout <<"Difference between now and next hour: "<< diff.total_seconds()<<" seconds"<< std::endl;// 获取当前系统时间,精确到毫秒
    boost::posix_time::ptime now_ms = boost::posix_time::microsec_clock::local_time();// 将时间转换为毫秒
    boost::posix_time::time_duration duration = now_ms.time_of_day();long milliseconds = duration.total_milliseconds();// 输出毫秒级时间
    std::cout <<"Current system milliseconds: "<< milliseconds << std::endl;long t1 =GetTime();sleep(1);long t2 =GetTime();// 输出时间差
    std::cout <<"This program cost: "<< t2 - t1 << std::endl;return0;}longGetTime(){
    boost::posix_time::ptime now_ms = boost::posix_time::microsec_clock::local_time();
    boost::posix_time::time_duration duration = now_ms.time_of_day();long milliseconds = duration.total_milliseconds();return milliseconds;}

编译运行:

g++ -o main main.cpp -lboost_date_time&& ./main
2816:52:31
Tomorrow: 2023-Jul-29 16:52:31
Next hour: 2023-Jul-28 17:52:31
Difference between now and next hour: 3600 seconds
Current system milliseconds: 60751420
This program cost: 1000

Boost.Geometry使用示例

// 计算两点间距离 -lboost_system -lboost_geometry#include<iostream>#include<boost/geometry.hpp>#include<boost/geometry/geometries/register/point.hpp>namespace bg = boost::geometry;// 定义一个 Point 结构体,并注册为 Boost.Geometry 的点类型structPoint{double x, y;};BOOST_GEOMETRY_REGISTER_POINT_2D(Point,double, bg::cs::cartesian, x, y)intmain(){// 创建两个点
    Point p1{0.0,0.0};
    Point p2{1.0,1.0};// 计算两个点之间的欧几里得距离double distance = bg::distance(p1, p2);

    std::cout <<"Distance between points: "<< distance << std::endl;return0;}
// 点集转线#include<iostream>#include<vector>#include<boost/geometry.hpp>#include<boost/geometry/geometries/point_xy.hpp>#include<boost/geometry/geometries/linestring.hpp>namespace bg = boost::geometry;typedef bg::model::d2::point_xy<double> Point;typedef bg::model::linestring<Point> LineString;intmain(){// 创建点集
    std::vector<Point> points;
    points.push_back(Point(0,0));
    points.push_back(Point(1,1));
    points.push_back(Point(2,2));
    points.push_back(Point(3,3));// 将点集转换为线
    LineString line;
    bg::assign_points(line, points);// 输出线的坐标
    std::cout <<"Line coordinates: ";for(auto it = boost::begin(line); it != boost::end(line);++it){
        std::cout << bg::get<0>(*it)<<" "<< bg::get<1>(*it)<<", ";}
    std::cout << std::endl;return0;}
// 面要素转线要素#include<iostream>#include<vector>#include<boost/geometry.hpp>#include<boost/geometry/geometries/polygon.hpp>#include<boost/geometry/geometries/linestring.hpp>namespace bg = boost::geometry;typedef bg::model::polygon<bg::model::d2::point_xy<double>> Polygon;typedef bg::model::linestring<bg::model::d2::point_xy<double>> LineString;voidpolygonToLineString(const Polygon& polygon, LineString& lineString){constauto& outerRing = bg::exterior_ring(polygon);
    bg::append(lineString, outerRing);for(constauto& innerRing : bg::interior_rings(polygon)){
        bg::append(lineString, innerRing);}}intmain(){// 创建一个多边形
    Polygon polygon;
    bg::read_wkt("POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,2 8,8 8,8 2,2 2))", polygon);// 将多边形转换为线
    LineString lineString;polygonToLineString(polygon, lineString);// 输出线上的点
    std::cout <<"Line points: ";for(constauto& point : lineString){
        std::cout <<"("<< bg::get<0>(point)<<" "<< bg::get<1>(point)<<"), ";}
    std::cout << std::endl;return0;}
// 线要素转点要素#include<iostream>#include<vector>#include<boost/geometry.hpp>#include<boost/geometry/geometries/point.hpp>#include<boost/geometry/geometries/linestring.hpp>namespace bg = boost::geometry;typedef bg::model::point<double,2, bg::cs::cartesian> Point;typedef bg::model::linestring<Point> LineString;voidlineStringToPoints(const LineString& lineString, std::vector<Point>& points){for(constauto& point : lineString){
        points.push_back(point);}}intmain(){// 创建一个线要素
    LineString lineString;
    lineString.push_back(Point(0,0));
    lineString.push_back(Point(1,1));
    lineString.push_back(Point(2,2));// 将线要素转换为点
    std::vector<Point> points;lineStringToPoints(lineString, points);// 输出点的坐标
    std::cout <<"Point coordinates: ";for(constauto& point : points){
        std::cout <<"("<< bg::get<0>(point)<<" "<< bg::get<1>(point)<<"), ";}
    std::cout << std::endl;return0;}

在这里插入图片描述

以上。

标签: c++ 开源 开发语言

本文转载自: https://blog.csdn.net/qq_40344790/article/details/131980629
版权归原作者 DevFrank 所有, 如有侵权,请联系我们删除。

“【C++】开源:Boost库常用组件配置使用”的评论:

还没有评论