??★,°:.☆( ̄▽ ̄)/$:.°★ ??
這篇文章主要介紹Boost庫常用組件配置使用。
無專精則不能成,無涉獵則不能通?!簡⒊?/font>
歡迎來到我的博客,一起學習,共同進步。
喜歡的朋友可以關注一下,下次更新不迷路??
??1. 項目介紹
項目Github地址:https://github.com/boostorg/boost
Boost庫在線書籍:https://wizardforcel.gitbooks.io/the-boost-cpp-libraries/content/0.html
Boost是一個流行的、開源的C++庫集合,提供了各種功能強大的庫和工具,擴展了C++語言的能力,并為開發(fā)者提供了更高級別的抽象和工具。Boost庫經(jīng)過廣泛的使用和測試,被認為是C++社區(qū)的事實標準之一。
Boost庫包含了多個模塊,每個模塊都提供了不同領域的功能和工具,覆蓋了諸如字符串操作、數(shù)據(jù)結構、算法、日期時間處理、文件系統(tǒng)、線程、網(wǎng)絡、正則表達式等各個方面。以下是一些常用的Boost庫:
1.Boost.Asio:提供了異步I/O操作的網(wǎng)絡編程庫,支持TCP、UDP、串口等網(wǎng)絡協(xié)議。
2.Boost.Smart_Ptr:提供了智能指針類,如shared_ptr和weak_ptr,用于方便地進行內存管理。
3.Boost.Filesystem:提供了對文件系統(tǒng)的訪問和操作,包括文件和目錄的創(chuàng)建、刪除、遍歷等。
4.Boost.Regex:提供了正則表達式的功能,用于進行文本匹配和搜索操作。
5.Boost.Thread:提供了跨平臺的多線程編程接口,簡化了線程的創(chuàng)建、同步和通信等操作。
6.Boost.Serialization:提供了對象的序列化和反序列化功能,可以將對象以二進制或XML格式進行存儲和傳輸。
除了以上列舉的庫之外,Boost還包含了許多其他功能豐富的庫,如Boost.Math用于數(shù)學計算、Boost.Graph用于圖論算法、Boost.Test用于單元測試等。Boost庫通常以頭文件方式提供,使用Boost只需包含相應的頭文件,并鏈接對應的庫文件。
Boost庫的目標是提供高質量和高可移植性的C++代碼,因此它的代碼質量很高,并且支持各種主流操作系統(tǒng)和編譯器。Boost庫的開發(fā)是一個開放的社區(qū)驅動過程,接受用戶的反饋和貢獻,并定期發(fā)布新版本。
Boost.Thread特性
線程管理:Boost.Thread可以創(chuàng)建、啟動、停止和管理線程。它提供了線程對象(boost::thread)來表示一個線程,并提供了一些類似于啟動線程、等待線程結束、檢查線程狀態(tài)等方法。
互斥鎖和條件變量:Boost.Thread 提供了互斥鎖和條件變量等同步原語,用于實現(xiàn)線程之間的互斥和同步?;コ怄i可以保護共享資源的訪問,條件變量可以實現(xiàn)線程之間的等待和通知機制。
線程間數(shù)據(jù)共享:Boost.Thread提供了一些線程間數(shù)據(jù)共享的機制,如原子操作、線程局部存儲等,可以保證在多線程環(huán)境下的數(shù)據(jù)訪問的正確性和一致性。
線程間通信:Boost.Thread 還提供了一些線程間通信的機制,如消息隊列、信號量等,可以實現(xiàn)線程之間的信息傳遞和同步。
并發(fā)算法:Boost.Thread 還提供了一些并發(fā)算法,如并行循環(huán)(parallel loop)、并行排序(parallel sort)等,可以在多核處理器上有效地執(zhí)行并行計算任務。
Boost.Serialization特性
序列化:Boost.Serialization 可以將對象序列化為字節(jié)流。通過使用 boost::serialization 命名空間中的 << 運算符,您可以將對象寫入輸出流中。
反序列化:Boost.Serialization 可以從字節(jié)流反序列化對象。通過使用 boost::serialization 命名空間中的 >> 運算符,您可以從輸入流中讀取字節(jié)并重建對象。
版本控制:Boost.Serialization 支持版本控制,可以在不同版本之間進行對象的序列化和反序列化。這使得改變對象的結構時可以進行向前和向后兼容。
對象關聯(lián):Boost.Serialization 能夠正確地處理對象之間的關聯(lián)關系和引用。當序列化一個對象時,被引用的對象也會被自動序列化,并在反序列化時進行恢復。
自定義擴展:Boost.Serialization 允許開發(fā)者對自定義類型進行擴展和適配,以支持序列化和反序列化操作。通過為自定義類型添加 serialize 函數(shù),可以指定如何將對象轉換為字節(jié)流和從字節(jié)流中恢復。
Boost.Math特性
數(shù)字運算:Boost.Math 提供了大量的數(shù)學函數(shù),例如冪函數(shù)、指數(shù)函數(shù)、對數(shù)函數(shù)、三角函數(shù)、雙曲函數(shù)等。這些函數(shù)支持各種數(shù)據(jù)類型,包括整數(shù)、浮點數(shù)和復數(shù),并且具有高精度和高效率。
特殊函數(shù):Boost.Math 實現(xiàn)了許多特殊函數(shù),如伽瑪函數(shù)、貝塞爾函數(shù)、橢圓積分、誤差函數(shù)和球貝塞爾函數(shù)等。這些函數(shù)在科學計算、信號處理、概率統(tǒng)計和物理建模等領域中具有廣泛的應用。
數(shù)值常量:Boost.Math 提供了許多常用的數(shù)學常量,如圓周率 π、自然對數(shù)底 e、黃金比例 φ 等。這些常量可以直接在代碼中使用,而無需手動輸入。
概率分布:Boost.Math 實現(xiàn)了各種概率分布函數(shù)和隨機數(shù)生成器,如正態(tài)分布、均勻分布、泊松分布和二項分布等。這些函數(shù)和生成器可用于模擬實驗、數(shù)據(jù)分析和統(tǒng)計推斷等應用場景。
統(tǒng)計算法:Boost.Math 包含一些統(tǒng)計計算的算法,如平均值、標準差、方差、協(xié)方差和相關系數(shù)等。這些算法可以用于描述和分析數(shù)據(jù)集的統(tǒng)計特性。
幾何計算:Boost.Math 提供了一些用于幾何計算的函數(shù)和類,如點、向量、矩陣、線段、射線和多邊形等。這些工具可以用于解決幾何問題,如交點計算、距離計算和形狀檢測等。
Boost.Time特性
boost::posix_time:提供了對時間點和時間間隔進行操作的類和函數(shù)。它支持高精度的時間表示,并提供了各種算術和比較運算符,以及格式化和解析時間的能力。
boost::gregorian:提供了對 Gregorian 陽歷日期進行操作的類和函數(shù)。它支持日期的算術和比較運算符,以及格式化和解析日期的能力。它還提供了一些有用的函數(shù),如計算某個日期的下一個工作日、計算某個月份的天數(shù)等。
boost::date_time:提供了一個更高級的日期和時間處理框架,可以處理多種不同的日歷系統(tǒng)、時區(qū)和時間精度。它建立在 boost::posix_time 和 boost::gregorian 的基礎上,提供了更豐富的功能。例如,它支持多種不同的日歷系統(tǒng),如 Julian 日歷、季節(jié)日歷等;支持多種不同的時區(qū)表示和轉換;還提供了更復雜的日期和時間算法,如計算某個日期之前或之后的工作日,計算某個日期所在的周是當年的第幾周等。
Boost.Geometry幾何計算庫特性
幾何數(shù)據(jù)模型:Boost.Geometry 定義了一套通用的幾何數(shù)據(jù)模型,包括點、線、多邊形等。這個數(shù)據(jù)模型可以適用于二維和三維空間,并支持不同的幾何類型。
幾何算法:Boost.Geometry 提供了許多幾何算法,包括距離計算、相交檢測、包圍盒計算、緩沖區(qū)計算等。這些算法可以應用于幾何對象上,以解決各種幾何問題。
幾何運算:Boost.Geometry 支持各種幾何運算,如交集、并集、差集、對稱差集等。這些運算可以用于組合和修改幾何對象。
空間索引:Boost.Geometry 提供了一些空間索引數(shù)據(jù)結構,如 R-tree 和 Quadtree,用于高效地進行空間查詢和搜索。
輸入/輸出支持:Boost.Geometry 支持各種幾何數(shù)據(jù)格式的輸入和輸出,包括 WKT (Well-Known Text)、WKB (Well-Known Binary) 等。這使得與其他幾何庫和工具進行數(shù)據(jù)交換變得更加容易。
??2. 環(huán)境配置
下面進行環(huán)境配置:
# apt安裝常用模塊
sudo apt-get install 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 install
sudo apt install libboost-all-dev
# 驗證高版本安裝
ls /usr/local/include/boost/geometry/
??3. 使用說明
下面進行使用分析:
Boost.Thread使用示例
創(chuàng)建線程示例:
#include <iostream>
#include <boost/thread.hpp>
// 線程函數(shù)
void threadFunction()
{
// 輸出線程相關信息
std::cout << "Thread ID: " << boost::this_thread::get_id() << std::endl;
std::cout << "Hello from a thread!" << std::endl;
}
int main()
{
// 創(chuàng)建線程并啟動
boost::thread threadObj(threadFunction);
// 多個線程類似
// 等待線程結束
threadObj.join();
// 輸出主線程相關信息
std::cout << "Thread ID: " << boost::this_thread::get_id() << std::endl;
std::cout << "Main thread exiting..." << std::endl;
return 0;
}
編譯運行:
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>
// 要進行序列化和反序列化的示例類
class MyClass
{
public:
int data;
double d;
std::string str;
// 聲明 Boost 序列化函數(shù)為友元函數(shù)
friend class boost::serialization::access;
// Boost 序列化函數(shù)(將對象轉換為字節(jié)流)
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & data;
ar & d;
ar & str;
}
};
int main()
{
// 創(chuàng)建一個 MyClass 對象并設置數(shù)據(jù)
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();
// 輸出反序列化后的對象數(shù)據(jù)
std::cout << "Restored data: " << restoredObj.data << std::endl;
std::cout << "Restored d: " << restoredObj.d << std::endl;
std::cout << "Restored str: " << restoredObj.str << std::endl;
return 0;
}
編譯運行:
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>
int main()
{
// 計算圓周率
double pi = boost::math::constants::pi<double>();
std::cout << "Pi: " << pi << std::endl;
// 貝塞爾函數(shù)
double besselJ0 = boost::math::cyl_bessel_j(0, 2.0);
std::cout << "Bessel J0(2.0): " << besselJ0 << std::endl;
return 0;
}
編譯運行:
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>
long GetTime();
int main()
{
// 獲取當前系統(tǒng)時間
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
std::cout << "Current system time: " << now << std::endl;
// 格式化輸出當前系統(tǒng)時間
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;
// 獲取當前系統(tǒng)時間,精確到毫秒
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;
return 0;
}
long GetTime()
{
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
28 16: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 的點類型
struct Point
{
double x, y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, bg::cs::cartesian, x, y)
int main()
{
// 創(chuàng)建兩個點
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;
return 0;
}
// 點集轉線
#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;
int main()
{
// 創(chuàng)建點集
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;
return 0;
}
// 面要素轉線要素
#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;
void polygonToLineString(const Polygon& polygon, LineString& lineString)
{
const auto& outerRing = bg::exterior_ring(polygon);
bg::append(lineString, outerRing);
for (const auto& innerRing : bg::interior_rings(polygon))
{
bg::append(lineString, innerRing);
}
}
int main()
{
// 創(chuàng)建一個多邊形
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 (const auto& point : lineString)
{
std::cout << "(" << bg::get<0>(point) << " " << bg::get<1>(point) << "), ";
}
std::cout << std::endl;
return 0;
}
// 線要素轉點要素
#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;
void lineStringToPoints(const LineString& lineString, std::vector<Point>& points)
{
for (const auto& point : lineString)
{
points.push_back(point);
}
}
int main()
{
// 創(chuàng)建一個線要素
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 (const auto& point : points)
{
std::cout << "(" << bg::get<0>(point) << " " << bg::get<1>(point) << "), ";
}
std::cout << std::endl;
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-614660.html
以上。文章來源地址http://www.zghlxwxcb.cn/news/detail-614660.html
到了這里,關于【C++】開源:Boost庫常用組件配置使用的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!