• <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>

            健康,快樂,勇敢的寧帥!!

            努力、努力、再努力! 沒有什么能阻止我對知識的渴望。

             

            "C++Templates The Complete Guide"讀書筆記----Chapter 3

            Chapter 3 Class Templates
            1. A clas template is a class that is implemented with one or more type parameters left open
            #include?<vector>
            #include?
            <stdexcept>

            template?
            <typename?T>
            class?Stack?{
            ??
            private:
            ????std::vector
            <T>?elems;?????//?elements

            ??
            public:
            ????
            void?push(T?const&);??????//?push?element
            ????void?pop();???????????????//?pop?element
            ????T?top()?const;????????????//?return?top?element
            ????bool?empty()?const?{??????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            template?
            <typename?T>
            void?Stack<T>::push?(T?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            template
            <typename?T>
            void?Stack<T>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            template?
            <typename?T>
            T?Stack
            <T>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }
            2. To use a class template, you pass the open types as template arguments. The class template is the instantiated(and compiled) for these types
            3. For class templates, only those menber funcitons that are called are instantiated
            This , of course, saves time and space. It has the additional benefit that you can instantiate a class even for those types that cannot perform all the operations of all the menber functions, as long as these member functions are not called.
            Note that you have to put whitespace between the two closing? template brackets. If you don't do this, you are using operator>>,which results in a syntax error:
            Stack<Stack<int>>?intStackStack;?//ERROR:>>is?not?allowed
            4. You can specialize calss templates for certain types
            template<>
            class?Stack<std::string>?{
            ??
            private:
            ????std::deque
            <std::string>?elems;??//?elements

            ??
            public:
            ????
            void?push(std::string?const&);??//?push?element
            ????void?pop();?????????????????????//?pop?element
            ????std::string?top()?const;????????//?return?top?element
            ????bool?empty()?const?{????????????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            void?Stack<std::string>::push?(std::string?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            void?Stack<std::string>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            std::
            string?Stack<std::string>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }


            ps:也就是所謂的特定版本,模板元編程中的關鍵。
            5. You can partially specialize class templates for certain types
            If more than one partial specilalization matches equally well, the declaration is ambiguous
            6. You can define default values for class template parameters.These may refer to previous template parameters

            posted on 2006-11-26 16:15 ningfangli 閱讀(166) 評論(0)  編輯 收藏 引用

            導航

            統計

            公告

            Dict.CN 在線詞典, 英語學習, 在線翻譯

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久频这里精品99香蕉久| 中文字幕精品无码久久久久久3D日动漫 | 99久久精品免费| 99久久国产免费福利| 久久综合成人网| 亚洲精品午夜国产va久久| 理论片午午伦夜理片久久 | 久久精品国产精品亚洲艾草网美妙| 色综合久久中文综合网| 久久天天躁狠狠躁夜夜2020| 久久九九兔免费精品6| 久久精品国产精品亚洲毛片| 一本久久久久久久| 亚洲国产精品无码久久一线 | 99久久99久久精品国产| 国产毛片欧美毛片久久久| 婷婷综合久久狠狠色99h| 久久久久亚洲av综合波多野结衣| 婷婷综合久久狠狠色99h| 色欲久久久天天天综合网精品| 94久久国产乱子伦精品免费| 国产精品无码久久综合| 久久AV高潮AV无码AV| 99精品国产在热久久无毒不卡| 伊人久久大香线蕉综合5g| 久久青草国产手机看片福利盒子| 久久久久av无码免费网| 久久夜色精品国产| 国产69精品久久久久99尤物| 国产午夜福利精品久久2021| 中文精品久久久久人妻不卡| 久久久久九国产精品| 久久精品18| 久久伊人中文无码| 九九热久久免费视频| 久久se这里只有精品| 国产日韩久久久精品影院首页| 欧美伊香蕉久久综合类网站| 国产一久久香蕉国产线看观看| 2022年国产精品久久久久| 久久免费国产精品一区二区|