• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            templates

            由于想要對STL的具體實現有一個清楚的認識,因此在此先溫習一下關于templates的相關知識。

            模版的使用,將不同類型的具體實現交給了編譯器去完成,從而實現了代碼量的縮減,以達到清晰鮮明的目的。

            如有個函數

               1: template <typename Type> // this is the template parameter declaration
               2: Type max(Type tX, Type tY)
               3: {
               4:     return (tX > tY) ? tX : tY;
               5: }

            當你在編譯程序時,編譯器遇到相應的調用

               1: int nValue = max(3, 7); // calls max(int, int)

            那么編譯器將會對模版函數進行一次復制,并將類型改成相應的int類型,實現template instance.

               1: int max(int tX, int tY)
               2: {
               3:     return (tX > tY) ? tX : tY;
               4: }

            在模版的使用中,除了具體類型可以外,還可以是表達式參數.

            表達式參數可以是下面這些值:

            1. 整型或是枚舉

            2. 對象的指針或是引用

            3. 函數的指針或是引用

            4. 類成員函數的指針或是引用

            如:

               1: template <typename T, int nSize> // nSize is the expression parameter
               2: class Buffer
               3: {
               4: private:
               5:     // The expression parameter controls the size of the array
               6:     T m_atBuffer[nSize];
               7:  
               8: public:
               9:     T* GetBuffer() { return m_atBuffer; }
              10:  
              11:     T& operator[](int nIndex)
              12:     {
              13:         return m_atBuffer[nIndex];
              14:     }
              15: };
              16:  
              17: int main()
              18: {
              19:     // declare an integer buffer with room for 12 chars
              20:     Buffer<int, 12> cIntBuffer;
              21:  
              22:     // Fill it up in order, then print it backwards
              23:     for (int nCount=0; nCount < 12; nCount++)
              24:         cIntBuffer[nCount] = nCount;
              25:  
              26:     for (int nCount=11; nCount >= 0; nCount--)
              27:         std::cout << cIntBuffer[nCount] << " ";
              28:     std::cout << std::endl;
              29:  
              30:     // declare a char buffer with room for 31 chars
              31:     Buffer<char, 31> cCharBuffer;
              32:  
              33:     // strcpy a string into the buffer and print it
              34:     strcpy(cCharBuffer.GetBuffer(), "Hello there!");
              35:     std::cout << cCharBuffer.GetBuffer() << std::endl;
              36:  
              37:     return 0;
              38: }

            模版中類型的特例化

            看一下下面這個類:

               1: using namespace std;
               2:  
               3: template <typename T>
               4: class Storage
               5: {
               6: private:
               7:     T m_tValue;
               8: public:
               9:     Storage(T tValue)
              10:     {
              11:          m_tValue = tValue;
              12:     }
              13:  
              14:     ~Storage()
              15:     {
              16:     }
              17:  
              18:     void Print()
              19:     {
              20:         std::cout << m_tValue << std::endl;;
              21:     }
              22: };

            如果采用下面的例子進行調用:

               1: int main()
               2: {
               3:     using namespace std;
               4:  
               5:     // Dynamically allocate a temporary string
               6:     char *strString = new char[40];
               7:  
               8:     // Ask user for their name
               9:     cout << "Enter your name: ";
              10:     cin >> strString;
              11:  
              12:     // Store the name
              13:     Storage<char*> strValue(strString);
              14:  
              15:     // Delete the temporary string
              16:     delete strString;
              17:  
              18:     // Print out our value
              19:     strValue.Print(); // This will print garbage
              20: }

            來看一下此時類的構造:

               1: Storage<char*>::Storage(char* tValue)
               2: {
               3:      m_tValue = tValue;
               4: }

            可以明顯的發現m_tValue和strString指向同一塊內存空間。

            采用delete將strString刪除后,使得m_tValue指向的內容也變成沒有定義的了。而上面這個類在對于int類型的時候能夠很好的實現。因此對于char*類型需要特殊對待。如下:

               1: Storage<char*>::Storage(char* tValue)
               2: {
               3:     // Allocate memory to hold the tValue string
               4:     m_tValue = new char[strlen(tValue)+1];
               5:     // Copy the actual tValue string into the m_tValue memory we just allocated
               6:     strcpy(m_tValue, tValue);
               7: }
               1: Storage<char*>::~Storage()
               2: {
               3:     delete[] m_tValue;
               4: }

            整個類的特例化:

               1: template <typename T>
               2: class Storage8
               3: {
               4: private:
               5:     T m_tType[8];
               6:  
               7: public:
               8:     void Set(int nIndex, const T &tType)
               9:     {
              10:         m_tType[nIndex] = tType;
              11:     }
              12:  
              13:     const T& Get(int nIndex)
              14:     {
              15:         return m_tType[nIndex];
              16:     }
              17: };

            上面這個類對于int的時候能夠很好的實現,但是遇到bool時,由于bool的信息只用一個位就能夠表示,而內存的最小存儲單元是一個字節,因此采用上述的類存儲bool的內存中有7/8是浪費的,因此采用另一種方式進行實現,構建一個新的類需要使用不用的變量名,使得使用的時候有一定的麻煩,因此采用了對整個類的特例化。

               1: template <> // the following is a template class with no templated parameters
               2: class Storage8<bool> // we're specializing Storage8 for bool
               3: {
               4: // What follows is just standard class implementation details
               5: private:
               6:     unsigned char m_tType;
               7:  
               8: public:
               9:     void Set(int nIndex, bool tType)
              10:     {
              11:         // Figure out which bit we're setting/unsetting
              12:         // This will put a 1 in the bit we're interested in turning on/off
              13:         unsigned char nMask = 1 << nIndex;
              14:  
              15:         if (tType)  // If we're setting a bit
              16:             m_tType |= nMask;  // Use bitwise-or to turn that bit on
              17:         else  // if we're turning a bit off
              18:             m_tType &= ~nMask;  // bitwise-and the inverse mask to turn that bit off
              19:     }
              20:  
              21:     bool Get(int nIndex)
              22:     {
              23:         // Figure out which bit we're getting
              24:         unsigned char nMask = 1 << nIndex;
              25:         // bitwise-and to get the value of the bit we're interested in
              26:         // Then implicit cast to boolean
              27:         return m_tType & nMask;
              28:     }
              29: };

            posted on 2012-07-09 14:48 鐘謝偉 閱讀(1010) 評論(0)  編輯 收藏 引用

            <2012年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久se色偷偷亚洲精品av| 久久久久久亚洲精品成人 | 久久精品中文字幕第23页| AV无码久久久久不卡网站下载| 伊人久久大香线蕉AV色婷婷色| 亚洲&#228;v永久无码精品天堂久久| 亚洲国产精品一区二区久久| 欧美激情精品久久久久| 久久青草国产精品一区| 久久综合丁香激情久久| 国产69精品久久久久777| 久久噜噜电影你懂的| 四虎国产精品免费久久5151| 久久美女人爽女人爽| 国产69精品久久久久99| 久久精品国产清自在天天线| 欧美午夜精品久久久久久浪潮| 日韩欧美亚洲综合久久影院Ds| 亚洲人成电影网站久久| 97久久婷婷五月综合色d啪蜜芽 | 久久精品国产99久久丝袜| 久久久久亚洲精品中文字幕 | 久久久无码精品亚洲日韩蜜臀浪潮| 亚洲精品无码专区久久同性男| 精品综合久久久久久97| 久久亚洲私人国产精品| 99久久精品国产一区二区蜜芽 | 日本欧美久久久久免费播放网| 97精品伊人久久大香线蕉app| 久久最近最新中文字幕大全 | 国产精品99久久不卡| 亚洲欧洲精品成人久久奇米网| 亚洲欧美伊人久久综合一区二区 | 久久狠狠一本精品综合网| 7777精品伊人久久久大香线蕉 | 久久精品国产亚洲5555| 亚洲乱码精品久久久久..| 97久久精品人人澡人人爽| 亚洲国产成人久久精品99| 91精品国产综合久久精品| 欧美国产精品久久高清|