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

            Chapter 4 Nontype Template Parameters
            1. Templates can have template parameters that are values rather than types
            With this class, user of the stack could specify this size of the array as the maximum size needed for stack elements
            template?<typename?T,?int?MAXSIZE>
            class?Stack?{
            ??
            private:
            ????T?elems[MAXSIZE];????????
            //?elements
            ????int?numElems;????????????//?current?number?of?elements

            ??
            public:
            ????Stack();??????????????????
            //?constructor
            ????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?numElems?==?0;
            ????}

            ????
            bool?full()?const?{???????//?return?whether?the?stack?is?full
            ????????return?numElems?==?MAXSIZE;
            ????}

            }
            ;

            //?constructor
            template?<typename?T,?int?MAXSIZE>
            Stack
            <T,MAXSIZE>::Stack?()
            ??:?numElems(
            0)???????????????//?start?with?no?elements
            {
            ????
            //?nothing?else?to?do
            }


            template?
            <typename?T,?int?MAXSIZE>
            void?Stack<T,MAXSIZE>::push?(T?const&?elem)
            {
            ????
            if?(numElems?==?MAXSIZE)?{
            ????????
            throw?std::out_of_range("Stack<>::push():?stack?is?full");
            ????}

            ????elems[numElems]?
            =?elem;???//?append?element
            ????++numElems;???????????????//?increment?number?of?elements
            }


            template
            <typename?T,?int?MAXSIZE>
            void?Stack<T,MAXSIZE>::pop?()
            {
            ????
            if?(numElems?<=?0)?{
            ????????
            throw?std::out_of_range("Stack<>::pop():?empty?stack");
            ????}

            ????
            --numElems;???????????????//?decrement?number?of?elements
            }


            template?
            <typename?T,?int?MAXSIZE>
            T?Stack
            <T,MAXSIZE>::top?()?const
            {
            ????
            if?(numElems?<=?0)?{
            ????????
            throw?std::out_of_range("Stack<>::top():?empty?stack");
            ????}

            ????
            return?elems[numElems-1];??//?return?last?element
            }
            2. You can also define nontype parameters for function templates.
            template?<typename?T,?int?VAL>
            T?addValue?(T?
            const&?x)
            {
            ????
            return?x?+?VAL;
            }
            However, according to the current standard, sets of overloaded functions cannot? be used for template parameter deduction. Thus, you have to cast to the exact type of the function template argument:
            std::transform(source.begin(),source.end(),//?start?and?end?of?source
            ???????????????dest.begin(),//?start?of?destination
            ???????????????(int(*)(int?const&))addValue<int,5>);//operation
            There is a proposal for the standard to fix this behavior so that the cast isn't necessary.
            2. You cannot use floating-point numbers, class-type objects, and objects with internal linkage(such as string literals) as arguments for nontype template parameters

            posted on 2006-11-26 19:50 ningfangli 閱讀(136) 評(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国产一区二区三区| 久久99精品久久久久婷婷| 97久久超碰成人精品网站| 香港aa三级久久三级老师2021国产三级精品三级在 | 婷婷伊人久久大香线蕉AV| 久久久青草久久久青草| 怡红院日本一道日本久久 | 理论片午午伦夜理片久久 | 国产高清美女一级a毛片久久w| 亚洲精品国产综合久久一线| 久久国产精品无码一区二区三区| 激情久久久久久久久久| 久久国产精品无码HDAV| 怡红院日本一道日本久久| 91精品国产色综久久| 99久久久精品免费观看国产| 少妇熟女久久综合网色欲| 久久精品无码一区二区三区免费| 国产午夜免费高清久久影院| 18禁黄久久久AAA片| 久久婷婷五月综合成人D啪| 99久久国产亚洲高清观看2024 | 2021国内精品久久久久久影院| 国产精品一区二区久久精品无码 | 久久91精品国产91久久小草| 亚洲综合伊人久久综合| 欧美伊人久久大香线蕉综合| 久久一区二区三区99| 亚洲精品高清国产一久久| 一本色道久久88加勒比—综合| 九九久久自然熟的香蕉图片| 久久久噜噜噜www成人网| 久久这里只有精品18| 久久久久无码精品国产| 久久久噜噜噜www成人网| 国产精品无码久久综合| 久久成人精品视频| 一本色道久久88加勒比—综合| 日本精品久久久中文字幕| 九九热久久免费视频|