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

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久久久天天影视| 久久精品国产亚洲AV麻豆网站 | 99久久精品免费看国产一区二区三区 | 999久久久国产精品| 久久精品国产清自在天天线| 久久国产劲爆AV内射—百度| 日韩精品国产自在久久现线拍 | 欧美日韩精品久久久久 | 亚洲午夜精品久久久久久浪潮| 久久精品国产精品青草| 久久水蜜桃亚洲av无码精品麻豆| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 99久久成人国产精品免费| 日本高清无卡码一区二区久久| 久久永久免费人妻精品下载| 久久国产视屏| 久久精品国产99国产电影网| 一本久久a久久精品综合香蕉 | 欧美亚洲国产精品久久| 欧美一级久久久久久久大片| 99久久无色码中文字幕| 中文字幕热久久久久久久| 综合人妻久久一区二区精品 | 看久久久久久a级毛片| 欧美久久久久久| 久久久久久午夜精品| 久久久久久国产精品无码下载| 国产午夜福利精品久久2021| 久久福利青草精品资源站| 日产精品久久久久久久性色| 久久久久亚洲AV无码专区首JN | 久久精品99久久香蕉国产色戒 | 免费精品久久久久久中文字幕| 老司机午夜网站国内精品久久久久久久久| 久久久久久九九99精品| 亚洲愉拍99热成人精品热久久| 久久无码一区二区三区少妇| 久久中文字幕无码专区| 人妻精品久久久久中文字幕| 久久激情五月丁香伊人| 久久国产成人精品国产成人亚洲|