1. 程式人生 > 其它 >C++篇:基礎入門(六)

C++篇:基礎入門(六)

技術標籤:c++程式語言

C++篇:基礎入門(六)

C++ 模板
模板是泛型程式設計的基礎,泛型程式設計即以一種獨立於任何特定型別的方式編寫程式碼。模板是建立泛型類或函式的藍圖或公式。庫容器,比如迭代器和演算法,都是泛型程式設計的例子,它們都使用了模板的概念。每個容器都有一個單一的定義,比如 向量,我們可以定義許多不同型別的向量,比如 vector 或 vector 。您可以使用模板來定義函式和類,接下來讓我們一起來看看如何使用。
模板函式定義的一般形式如下所示:

template <typename type> ret-type func-name(parameter list)
{ // 函式的主體 } 在這裡,type 是函式所使用的資料型別的佔位符名稱。這個名稱可以在函式定義中使用。

下面是函式模板的例項,返回兩個數中的最大值:


#include <iostream>
#include <string>
 
using namespace std;
 
template <typename T>
inline T const& Max (T const& a, T const& b) 
{ 
    return a < b ? b:a; 
} 
int main ()
{
 
    int
i = 39; int j = 20; cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5; double f2 = 20.7; cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"
; cout << "Max(s1, s2): " << Max(s1, s2) << endl; return 0; } 當上面的程式碼被編譯和執行時,它會產生下列結果: Max(i, j): 39 Max(f1, f2): 20.7 Max(s1, s2): World

類模板
正如我們定義函式模板一樣,我們也可以定義類模板。泛型類宣告的一般形式如下所示:

template <class type> class class-name {
.
.

}

在這裡,type 是佔位符型別名稱,可以在類被例項化的時候進行指定。您可以使用一個逗號分隔的列表來定義多個泛型資料型別。
下面的例項定義了類 Stack<>,並實現了泛型方法來對元素進行入棧出棧操作:


#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
 
using namespace std;
 
template <class T>
class Stack { 
  private: 
    vector<T> elems;     // 元素 
 
  public: 
    void push(T const&);  // 入棧
    void pop();               // 出棧
    T top() const;            // 返回棧頂元素
    bool empty() const{       // 如果為空則返回真。
        return elems.empty(); 
    } 
}; 
 
template <class T>
void Stack<T>::push (T const& elem) 
{ 
    // 追加傳入元素的副本
    elems.push_back(elem);    
} 
 
template <class T>
void Stack<T>::pop () 
{ 
    if (elems.empty()) { 
        throw out_of_range("Stack<>::pop(): empty stack"); 
    }
    // 刪除最後一個元素
    elems.pop_back();         
} 
 
template <class T>
T Stack<T>::top () const 
{ 
    if (elems.empty()) { 
        throw out_of_range("Stack<>::top(): empty stack"); 
    }
    // 返回最後一個元素的副本 
    return elems.back();      
} 
 
int main() 
{ 
    try { 
        Stack<int>         intStack;  // int 型別的棧 
        Stack<string> stringStack;    // string 型別的棧 
 
        // 操作 int 型別的棧 
        intStack.push(7); 
        cout << intStack.top() <<endl; 
 
        // 操作 string 型別的棧 
        stringStack.push("hello"); 
        cout << stringStack.top() << std::endl; 
        stringStack.pop(); 
        stringStack.pop(); 
    } 
    catch (exception const& ex) { 
        cerr << "Exception: " << ex.what() <<endl; 
        return -1;
    } 
}

當上面的程式碼被編譯和執行時,它會產生下列結果:
7
hello
Exception: Stack<>::pop(): empty stack

C++ 前處理器
前處理器是一些指令,指示編譯器在實際編譯之前所需完成的預處理。所有的前處理器指令都是以井號(#)開頭,只有空格字元可以出現在預處理指令之前。預處理指令不是 C++ 語句,所以它們不會以分號(;)結尾。我們已經看到,之前所有的例項中都有 #include 指令。這個巨集用於把標頭檔案包含到原始檔中。C++ 還支援很多預處理指令,比如 #include、#define、#if、#else、#line 等,讓我們一起看看這些重要指令。
#define 預處理
#define 預處理指令用於建立符號常量,該符號常量通常稱為巨集。
引數巨集
您可以使用 #define 來定義一個帶有引數的巨集,如下所示:

#include <iostream>
using namespace std;
 
#define MIN(a,b) (a<b ? a : b)
 
int main ()
{
   int i, j;
   i = 100;
   j = 30;
   cout <<"較小的值為:" << MIN(i, j) << endl;
 
    return 0;
}

條件編譯
有幾個指令可以用來有選擇地對部分程式原始碼進行編譯。這個過程被稱為條件編譯。條件前處理器的結構與 if 選擇結構很像。請看下面這段前處理器的程式碼:

#ifdef NULL
   #define NULL 0
#endif

您可以只在除錯時進行編譯,除錯開關可以使用一個巨集來實現,如下所示:

#ifdef DEBUG
   cerr <<"Variable x = " << x << endl;
#endif

讓我們嘗試下面的例項:


#include <iostream>
using namespace std;
#define DEBUG
 
#define MIN(a,b) (((a)<(b)) ? a : b)
 
int main ()
{
   int i, j;
   i = 100;
   j = 30;
#ifdef DEBUG
   cerr <<"Trace: Inside main function" << endl;
#endif
 
#if 0
   /* 這是註釋部分 */
   cout << MKSTR(HELLO C++) << endl;
#endif
 
   cout <<"The minimum is " << MIN(i, j) << endl;
 
#ifdef DEBUG
   cerr <<"Trace: Coming out of main function" << endl;
#endif
    return 0;
}

當上面的程式碼被編譯和執行時,它會產生下列結果:
Trace: Inside main function
The minimum is 30
Trace: Coming out of main function

#和 ## 運算子
#和 ## 預處理運算子在 C++ 和 ANSI/ISO C 中都是可用的。
#運算子會把 replacement-text 令牌轉換為用引號引起來的字串。
請看下面的巨集定義:

#include <iostream>
using namespace std;
 
#define MKSTR( x ) #x
 
int main ()
{
    cout << MKSTR(HELLO C++) << endl;
 
    return 0;
}

當上面的程式碼被編譯和執行時,它會產生下列結果:
HELLO C++

讓我們來看看它是如何工作的。不難理解,C++ 前處理器把下面這行:

cout << MKSTR(HELLO C++) << endl;

轉換成了:

cout << "HELLO C++" << endl;

##運算子用於連線兩個令牌。下面是一個例項:

#define CONCAT( x, y )  x ## y

當 CONCAT 出現在程式中時,它的引數會被連線起來,並用來取代巨集。例如,程式中 CONCAT(HELLO, C++) 會被替換為 “HELLO C++”,如下面例項所示。

#include <iostream>
using namespace std;
 
#define concat(a, b) a ## b
int main()
{
   int xy = 100;
   
   cout << concat(x, y);
   return 0;
}

當上面的程式碼被編譯和執行時,它會產生下列結果:
100

讓我們來看看它是如何工作的。不難理解,C++ 前處理器把下面這行:

cout << concat(x, y);

轉換成了:

cout << xy;

C++ 中的預定義巨集
C++ 提供了下表所示的一些預定義巨集:
在這裡插入圖片描述
讓我們看看上述這些巨集的例項:


#include <iostream>
using namespace std;
 
int main ()
{
    cout << "Value of __LINE__ : " << __LINE__ << endl;
    cout << "Value of __FILE__ : " << __FILE__ << endl;
    cout << "Value of __DATE__ : " << __DATE__ << endl;
    cout << "Value of __TIME__ : " << __TIME__ << endl;
 
    return 0;
}



當上面的程式碼被編譯和執行時,它會產生下列結果:
Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Feb 28 2011
Value of __TIME__ : 18:52:48