背景 公司的海外業務需要將雲上的操作讀取到內部的日誌文件中,永久保存,供內部審計使用。 由於之前沒有用過AWS相關的SDK,在使用過程中也遇到一些困難,這裡記錄一下,並且總結一下過程。 代碼 快速開始 代碼參考地址:https://github.com/awsdocs/aws-doc-sdk-exa ...
1) <limits>庫:
1.1 源文檔:
https://en.cppreference.com/w/cpp/types/numeric_limits
#include <limits>
1.2 庫函數:
函數解釋:
對於一個浮點數,lowest表示最小的可表示的負數,min表示最小的可表示的接近0的數,max表示最大的可表示的正數
對於一個有符號整數,min表示可以表示的最小的負數,max表示可以表示的最大的證書
std::cout << "The range for short is from " << std::numeric_limits<short>::min() << " to "
<< std::numeric_limits<short>::max() << std::endl;
std::cout << "The range for unsigned short is from " << std::numeric_limits<unsigned short>::min() << " to "
<< std::numeric_limits<unsigned short>::max() << std::endl;
std::cout << "The range for int is from " << std::numeric_limits<int>::min() << " to "
<< std::numeric_limits<int>::max() << std::endl;
std::cout << "The range for unsigned int is from " << std::numeric_limits<unsigned int>::min() << " to "
<< std::numeric_limits<unsigned int>::max() << std::endl;
std::cout << "The range for long is from " << std::numeric_limits<long>::min() << " to "
<< std::numeric_limits<long>::max() << std::endl;
std::cout << "The range for float is from " << std::numeric_limits<float>::min() << " to "
<< std::numeric_limits<float>::max() << std::endl;
std::cout << "The range(with lowest) for float is from " << std::numeric_limits<float>::lowest() << " to "
<< std::numeric_limits<float>::max() << std::endl;
std::cout << "The range(with lowest) for double is from " << std::numeric_limits<double>::lowest() << " to "
<< std::numeric_limits<double>::max() << std::endl;
std::cout << "The range(with lowest) for long double is from " << std::numeric_limits<long double>::lowest() << " to "
<< std::numeric_limits<long double>::max() << std::endl;
//Other facilities
//More info : https://en.cppreference.com/w/cpp/types/numeric_limits
std::cout << "int is signed : " << std::numeric_limits<int>::is_signed << std::endl;
std::cout << "int digits : " << std::numeric_limits<int>::digits << std::endl; //digits is the number of digits in base-radix that can be represented by the type T without change. For integer types, this is the number of bits not counting the sign bit and the padding bits
輸出結果:
The range for short is from -32768 to 32767
The range for unsigned short is from 0 to 65535
The range for int is from -2147483648 to 2147483647
The range for float is from 1.17549e-38 to 3.40282e+38
The range(with lowest) for float is from -3.40282e+38 to 3.40282e+38
The range(with lowest) for double is from -1.79769e+308 to 1.79769e+308
The range(with lowest) for long double is from -1.18973e+4932 to 1.18973e+4932
int is signed : 1
int digits : 31
2)<cmath>庫
2.1 源文檔:
#include <cmath>
https://en.cppreference.com/w/cpp/header/cmath
2.2 部分庫函數:
std::abs(a): 絕對值
std::exp(a): e的乘方
std::pow(a,b): a的b次方
std::log(a): e的對數
std::log10(a): 10的對數
std::sqrt(a): 開平方根
std::round(a): 四捨五入
三角函數(單位是弧度制):sin(), sinf(float num), sinl(long double number)
2.3 對char類型和short int類型的數學計算:
編譯器無法處理小於4bytes的數據的計算,char類型占據1 Byte,short int類型占據2 Bytes, 在進行運算時會自動轉換為int類型
short int var1 {10}; // 2 bytes
short int var2 {20};
char var3 {40}; //1
char var4 {50};
std::cout << "size of var1 : " << sizeof(var1) << std::endl;
std::cout << "size of var2 : " << sizeof(var2) << std::endl;
std::cout << "size of var3 : " << sizeof(var3) << std::endl;
std::cout << "size of var4 : " << sizeof(var4) << std::endl;
auto result1 = var1 + var2 ;
auto result2 = var3 + var4;
std::cout << "size of result1 : " << sizeof(result1) << std::endl; // 4
std::cout << "size of result2 : " << sizeof(result2) << std::endl; // 4