• <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 鐘謝偉 閱讀(1014) 評論(0)  編輯 收藏 引用

            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品一区二区久久不卡| 色播久久人人爽人人爽人人片aV | av色综合久久天堂av色综合在| 国产精品综合久久第一页| yellow中文字幕久久网| 夜夜亚洲天天久久| 亚洲婷婷国产精品电影人久久| 久久久久人妻一区精品性色av | 国产成人香蕉久久久久| 精品国产乱码久久久久久浪潮| 久久亚洲sm情趣捆绑调教| 久久国产精品99精品国产987| 欧美久久一区二区三区| 中文国产成人精品久久不卡 | 久久播电影网| 99久久做夜夜爱天天做精品| 国产麻豆精品久久一二三| 2021最新久久久视精品爱| 999久久久无码国产精品| 18禁黄久久久AAA片| 久久最近最新中文字幕大全| 一本久久a久久精品亚洲| 精品无码久久久久久久久久| 欧美一区二区三区久久综 | 97久久国产露脸精品国产| 99久久免费国产精品热| 国产精品中文久久久久久久| 四虎国产精品免费久久5151| 久久热这里只有精品在线观看| 日韩一区二区三区视频久久| 久久91精品久久91综合| 亚洲欧美成人综合久久久| 亚洲AV无码成人网站久久精品大| 精品久久久久久国产三级| 97久久超碰国产精品旧版| 国内高清久久久久久| 亚洲一级Av无码毛片久久精品| 久久精品国产一区二区三区| 亚洲精品综合久久| 久久人搡人人玩人妻精品首页| 无码国内精品久久人妻麻豆按摩|