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

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

            努力、努力、再努力! 沒有什么能阻止我對(duì)知識(shí)的渴望。

             

            "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:也就是所謂的特定版本,模板元編程中的關(guān)鍵。
            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) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            統(tǒng)計(jì)

            公告

            Dict.CN 在線詞典, 英語學(xué)習(xí), 在線翻譯

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲国产精品成人久久蜜臀| 久久综合色区| 99久久超碰中文字幕伊人| 久久精品aⅴ无码中文字字幕不卡| 国产精品国色综合久久| 久久国产精品无码网站| 午夜天堂精品久久久久| 国产精品九九久久免费视频 | 久久久久久久国产免费看| 久久人人爽人人爽人人片AV高清| 久久精品国产亚洲精品2020| 久久午夜福利电影| 九九99精品久久久久久| 久久久久亚洲精品日久生情 | 久久久久一区二区三区| 亚洲国产成人精品女人久久久 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 色8久久人人97超碰香蕉987| 国内精品久久久久久中文字幕| 精品久久久无码21p发布| 国产高清美女一级a毛片久久w| 人人狠狠综合久久88成人| 一本久久精品一区二区| 久久se这里只有精品| 日本久久久久久中文字幕| 久久久久亚洲AV无码麻豆| 久久99热这里只有精品国产| 久久精品国产72国产精福利| 国产精品久久久久久久久免费| 波多野结衣久久一区二区| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 99热精品久久只有精品| 欧美黑人又粗又大久久久| 色欲久久久天天天综合网| 久久精品国产乱子伦| 久久国内免费视频| 欧美日韩精品久久久免费观看| 亚洲欧洲久久久精品| 亚洲日本va午夜中文字幕久久 | 91久久精一区二区三区大全| 国产精品禁18久久久夂久|