標籤:style 使用 os 資料 io c++
說白了,它是一個模板類,它主要是把C++當中的一些內建型別進行了封裝,比如說numeric_limits<int>是一個特化後的類,從這個類的成員變數與成員函數中,我們可以瞭解到int的很多特性:可以表示的最大值,最小值,是否是精確的,是否是有符號等等。如果用其他任意(非內建類型)來特化這個模板類,比如string,string怎麼可能有最大值?我們從MSDN上可以瞭解到,這對string,成員變數與成員函數是沒有意義的,要麼返回0要麼為false。
具體的一些用法:
#include <limits>
#include <iostream>
using namespace std;
int main() {
cout << boolalpha;
cout << "max(short): " << numeric_limits<short>::max() << endl;
cout << "min(short): " << numeric_limits<short>::min() << endl;
cout << "max(int): " << numeric_limits<int>::max() << endl;
cout << "min(int): " << numeric_limits<int>::min() << endl;
cout << "max(long): " << numeric_limits<long>::max() << endl;
cout << "min(long): " << numeric_limits<long>::min() << endl;
cout << endl;
cout << "max(float): " << numeric_limits<float>::max() << endl;
cout << "min(float): " << numeric_limits<float>::min() << endl;
cout << "max(double): " << numeric_limits<double>::max() << endl;
cout << "min(double): " << numeric_limits<double>::min() << endl;
cout << "max(long double): " << numeric_limits<long double>::max() << endl;
cout << "min(long double): " << numeric_limits<long double>::min() << endl;
cout << endl;
cout << "is_signed(char): "
<< numeric_limits<char>::is_signed << endl;
cout << "is_specialized(string): "
<< numeric_limits<string>::is_specialized << endl;
}
運行結果:
max(short): 32767
min(short): -32768
max(int): 2147483647
min(int): -2147483648
max(long): 2147483647
min(long): -2147483648
max(float): 3.40282e+038
min(float): 1.17549e-038
max(double): 1.79769e+308
min(double): 2.22507e-308
max(long double): 1.79769e+308
min(long double): 2.22507e-308
is_signed(char): true
is_specialized(string): false
請按任意鍵繼續. . .
關於為什麼float的最小值竟然是正的?我也存在疑問,從結果中,我們看出,min返回的是float型別可以表示的最小的正值,
而不是最小的float數。
從這個例子中,我們差不多瞭解到numeric_limits的基本用法。
3. 基本成員函數
我以float類型來展示:
#include <limits>
#include <iostream>
using namespace std;
int main() {
cout << boolalpha;
// 可以表示的最大值
cout << "max(float): " << numeric_limits<float>::max() << endl;
// 可以表示的大於0的最小值,其他類型的實現或與此不同
cout << "min(float): " << numeric_limits<float>::min() << endl;
// 標準庫是否為其實現了特化
cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;
// 是否是有符號的,即可以表示正負值
cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;
// 不否是整形的
cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;
// 是否是精確表示的
cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;
// 是否存在大小界限
cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;
// 兩個比較大的數相加而不會溢出,產生一個較小的值
cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;
// 是否符合某某標準
cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;
// 不加+-號可以表示的位元
cout << "digits(float): " << numeric_limits<float>::digits << endl;
// 十進位數的個數
cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;
// 一般基數為2
cout << "radix(float): " << numeric_limits<float>::radix << endl;
// 以2為基數的最小指數
cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;
// 以2為基數的最大指數
cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;
// 以10為基數的最小指數
cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;
// 以10為基數的最大指數
cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;
// 1值和最接近1值的差距
cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;
// 舍入方式
cout << "round_style(float): " << numeric_limits<float>::round_style << endl;
}
運行結果:
max(float): 3.40282e+038
min(float): 1.17549e-038
is_specialized(float): true
is_signed(float): true
is_integer(float): false
is_exact(float): false
is_bounded(float): true
is_modulo(float): false
is_iec559(float): true
digits(float): 24
digits10(float): 6
radix(float): 2
min_exponent(float): -125
max_exponent(float): 128
min_exponent10(float): -37
max_exponent10(float): 38
epsilon(float): 1.19209e-007
round_style(float): 1
請按任意鍵繼續. . .
Vector用於儲存物件數組
常用方法
1.push_back 在數組的最後添加一個資料
2.pop_back 去掉數組的最後一個資料
3.at 得到編號位置的資料
4.begin 得到數組頭的指標
5.end 得到數組的最後一個單元+1的指標
6.front 得到數組頭的引用
7.back 得到數組的最後一個單元的引用
8.max_size 得到vector最大可以是多大
9.capacity 當前vector分配的大小
10.size 當前使用資料的大小
11.resize 改變當前使用資料的大小,如果它比當前使用的大,者填充預設值
12.reserve 改變當前vecotr所分配空間的大小
13.erase 刪除指標指向的資料項目
14.clear 清空當前的vector
15.rbegin 將vector反轉後的開始指標返回(其實就是原來的end-1)
16.rend 將vector反轉構的結束指標返回(其實就是原來的begin-1)
17.empty 判斷vector是否為空白
18.swap 與另一個vector交換資料