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

            導航

            統計

            公告

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

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日韩人妻无码精品久久免费一| 亚洲&#228;v永久无码精品天堂久久 | 欧美国产成人久久精品| 综合久久给合久久狠狠狠97色| 久久精品国产久精国产一老狼| 人妻丰满AV无码久久不卡 | 久久免费线看线看| 久久婷婷五月综合成人D啪| 亚洲成色www久久网站夜月| 久久99国产精品久久久| 亚洲精品美女久久久久99小说| 久久精品国产亚洲AV无码麻豆| 久久久久久国产精品免费免费| 亚洲国产精品无码久久一区二区| 日韩精品国产自在久久现线拍| 伊人久久五月天| 久久香蕉综合色一综合色88| 久久精品国产亚洲AV蜜臀色欲| 久久精品成人免费看| 伊人久久大香线蕉av不变影院| 国内精品久久久久久久影视麻豆| 午夜不卡久久精品无码免费 | 午夜精品久久久久久久无码| 久久精品国产亚洲网站| 亚洲伊人久久精品影院| 久久精品夜色噜噜亚洲A∨| 成人资源影音先锋久久资源网| 亚洲精品99久久久久中文字幕 | 欧美日韩精品久久久免费观看| 国产精品福利一区二区久久| 欧美精品国产综合久久| 日本精品久久久久影院日本| 久久精品无码一区二区三区| 久久亚洲精品无码AV红樱桃| 久久精品卫校国产小美女| 久久人人爽人人精品视频| 久久精品夜色噜噜亚洲A∨| 国产精品99久久久久久宅男| 伊人久久综合热线大杳蕉下载| 久久国产精品国产自线拍免费| 国产情侣久久久久aⅴ免费|