1. 程式人生 > 實用技巧 >32,初探c++標準庫

32,初探c++標準庫

1.有趣的過載

(1)操作符<<原義是按位左移過載“<<”可將變數或常量左移到物件中

過載左移操作符(仿cout類)

 1 #include<stdio.h>
 2 
 3 const char endl = '\n';                  //將換行定義為一個常量
 4 
 5 class Console                           //Console表示命令列物件
 6 {
 7 public:
 8     Console& operator << (int i)                //
賦值過載函式,返回值變為Console&引用 9 { 10 printf("%d\n", i); 11 return *this; //當前物件自身返回,保證連續傳送值 12 } 13 14 Console& operator << (char c) //過載函式 15 { 16 printf("%c\n", c); 17 return *this; 18 } 19 Console& operator
<< (const char* s) //過載函式 20 { 21 printf("%s\n", s); 22 return *this; 23 } 24 Console& operator << (double d) //過載函式 25 { 26 printf("%f\n", d); 27 return *this; 28 } 29 30 }; 31 32 Console cout; 33 34 int main()
35 { 36 ////cout.operator<<(1); 37 //cout << 1; //將1的整數左移到cout 38 //cout << '\n'; //換行 39 // cout << 1 << '\n'; //error,過載函式返回值是void不能連續左移,將返回值用 Console&實現 40 41 cout << 1 <<endl; //避免輸入字元\n 42 cout << "LOVE.you" << endl; 43 44 double a = 0.1; 45 double b = 0.2; 46 cout << a + b << endl; 47 48 49 return 0; 50 }

2. C++標準庫

(1)C++標準庫

  • C++標準庫並不是C++語言的一部分

  • C++標準庫是由類庫和函式庫組成的集合,std

  • C++標準庫定義的類和物件都位於std名稱空間

  • C++標準庫標頭檔案不帶.h字尾

  • C++標準庫涵蓋了C庫功能

(2)C++編譯環境的組成

          

C語言相容庫:標頭檔案帶.h,是C++編譯器提供商為推廣自己的產品,而提供的C相容庫(不是C++標準庫提供的)。

C++標準庫:如stringcstdio(注意,不帶.h)是C++標準庫提供的。使用時要用using namespace std找開名稱空間。

編譯器擴充套件庫:編譯器自己擴充套件的

(3)C++標準庫預定義的常用資料結構

  ①<bitset>、<set>、<deque>、<stack>、<list>、<vector>、<queue>、<map>

  ②<cstdio>、<cstring>、<cstdlib>、<cmath>——(C++標準庫提供的C相容庫!

【程式設計實驗】C++標準庫中的C庫相容(如cstdio)

 1   //不是c語言標準庫也不是c++標準庫,是c++廠商為了推廣自己的產品提高的c相容庫
 2 /*#include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<math.h> */   
 6 
 7 
 8 //C++標準庫提供的C相容庫
 9 #include<cstdio>
10 #include<cstring>
11 #include<cstdlib>
12 #include<cmath>             
13 
14 using  namespace std;    //使用標準庫要開啟,namespace std名稱空間
15 
16 int main()
17 {
18     //以下的程式碼是使用C++標準庫提供的C相容庫寫的,
19 
20     //從形式上看,與C程式碼寫的完全一樣,這就是C++
21 
22     //為C提供了很好的支援的例子!
23 
24     printf("holle world!\n");
25 
26     char* p = (char*)malloc(16);  
27 
28     //strcpy(p, "sjdewhfj");
29 
30     double a = 3;
31     double b = 4;
32     double c = sqrt(a * a + b * b);
33 
34     printf("c=%f\n", c);
35 
36     free(p);             
37 
38     return 0;
39 }

4. C++輸入輸出

  

 1 #include<iostream>
 2 #include<cmath>
 3 
 4 //基於c++標準庫實現,  不使用廠商提供的c語言相容庫
 5 
 6 using  namespace std;
 7 
 8 int main()
 9 {
10     //printf("hello world!");
11     cout << "hello world!" << endl;            //cout為全域性物件------輸出(過載左移操作符)
12                                                //endl---換行
13 
14 
15     double a = 0;
16     double b = 0;
17 
18     cout << "input a :";  
19     cin >> a;                    //cin為全域性物件------輸入(過載右移操作符)   鍵盤上的輸入傳送帶a
20 
21     cout << "input b :";
22     cin >> b;
23 
24     double c = sqrt(a * a + b * b);   
25     cout << "c=" << c << endl;       //將字串  "c=" 和c的結果輸出到cout物件上,也就是傳送到顯示器上,顯示器對應的是命令列
26 
27 
28 
29 
30     return 0;
31 }

5.小結

(1)C++標準庫是由類庫和函式庫組成的集合---------使用c++標準庫裡的類和函式只需要包含標頭檔案(不帶.h),裡面包含子庫實現了c語言的全部功能

(2)C++標準庫包含經典演算法資料結構實現

(3)C++標準庫涵蓋了C庫的功能(子庫實現)

(4)C++標準庫位於std名稱空間中