C++中的常用庫
1. cmath: 數(shù)學計算
#include <iostream> #include <cmath> using namespace std; int main () { // 數(shù)字定義 short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // 數(shù)學運算 cout << "sin(d) :" << sin(d) << endl; cout << "abs(i) :" << abs(i) << endl; cout << "floor(d) :" << floor(d) << endl; cout << "sqrt(f) :" << sqrt(f) << endl; cout << "pow( d, 2) :" << pow(d, 2) << endl; return 0; }
下面是一個關于生成隨機數(shù)的簡單實例。實例中使用了cime的time()函數(shù)來獲取系統(tǒng)時間的秒數(shù),通過調(diào)用cstdlib的rand() 函數(shù)來生成隨機數(shù):
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; srand( (unsigned)time( NULL ) ); for(int i = 0; i < 10; i++ ) { // 生成實際的隨機數(shù) int j= rand(); cout <<"隨機數(shù): " << j << endl; }
如果使用相同的種子后面的rand()函數(shù)會出現(xiàn)一樣的隨機數(shù)。如:srand(1)
可以在宏定義中頂一個random(int number)函數(shù):#define random(x)(rand()%x)
2. iomanip:格式化輸出
#include<iostream> #include<iomanip> using namespace std; int main(){ double x=3.1415926; cout<<fixed<<setprecision(3)<<x<<endl; return 0; }
3. nlohmann json:json解析
nlohmann/json 是一個用于解析json的開源c++庫,口碑一流,號稱有業(yè)界最好的性能,并且使用非常方便直觀,是很多c++程序員的首選。
下載 https://github.com/nlohmann/json/tree/develop/single_include/nlohmann/json.hpp, 并加入本地工程。使用方式如下:
4. opencv:圖像處理
這里直接用了openvino里面帶的opencv。將其拷貝到3rdparty文件夾下,然后在CMakeLists中添加如下四行:
下面是使用例子:
5. openblas:矩陣計算
mac自帶了openblas,在/usr/local/Cellar下。將其拷貝到項目文件夾下,在cmakelists里添加即可。
關于api的文檔參照這里:
https://blog.csdn.net/weixin_43800762/article/details/87811697
首先是關鍵字:
接下來是:
到此這篇關于C++中的常用庫的文章就介紹到這了,更多相關C++常用庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++圖像加載之libpng、FreeImage、stb_image詳解
libpng、FreeImage、stb_image都是圖像解析的開源庫,這篇文章主要為大家詳細介紹了這三者的使用方法,文中的示例代碼講解詳細,需要的可以參考一下2023-06-06OpenCV數(shù)字圖像處理基于C++之圖像形態(tài)學處理詳解
OpenCV是一款由Intel公司俄羅斯團隊發(fā)起并參與和維護的一個計算機視覺處理開源軟件庫,支持與計算機視覺和機器學習相關的眾多算法,下面這篇文章主要給大家介紹了關于OpenCV數(shù)字圖像處理基于C++之圖像形態(tài)學處理的相關資料,需要的朋友可以參考下2022-12-12