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

            2009年12月21日

            條款2:盡量以CONST,ENUM,INLINE替換#DEFINE

            類中定義常量:static const int numturns = 5;

            這是聲明式而非定義式。若不需取它們的地址,無(wú)須提供定義式。定義式如下:

            const int GamePlayer::NumTurns;



            一個(gè)class需要常量值,定義如下:

            enum{NumTurns = 5};

            int scores[NumTurns];



            關(guān)于“形似函數(shù)的宏,最好改用inline函數(shù)替換#defines”

            但,經(jīng)驗(yàn)證明。宏的速度要比用inline的方法快。

            在一些對(duì)速度要求非常嚴(yán)格的項(xiàng)目中,這個(gè)方面還是要有所取舍的。

            posted @ 2009-12-21 00:18 蜜蜂 閱讀(234) | 評(píng)論 (1)編輯 收藏

            vector

            #include <vector>
            #include 
            <string>
            #include 
            <algorithm>
            #include 
            <iterator>
            #include 
            <iostream.h>
            // #include <iostream>

            class GreaterThan {
            public:
                GreaterThan( 
            int size = 6 ) : _size( size ){}
                
            int size() { return _size; }

                
            bool operator()(const string & s1) {
                    
            return s1.size() > 6;
                    }

            private:
                
            int _size;
            };

            template 
            <class Type>
            class PrintElem {
            public:
                
            void operator()( const Type &elem ) 
                {
                    
            ++_cnt;
                    
            if ( _cnt % 8 == 0 ) { cout << endl; }
                    cout 
            << elem << " ";
                }
                
            private:
                
            static int _cnt;
            };

            template 
            < class Type >
                   
            int PrintElem<Type>::_cnt = 0;

            class LessThan {
            public:
                
            bool operator()(const string & s1, const string & s2 ) {
                     
            return s1.size() < s2.size();
                    }
            };

            typedef vector
            <string, allocator> textwords;

            void process_vocab( vector<textwords, allocator> *pvec )
            {
                
            if ( ! pvec ) 
                     
            // issue warning message
                     return;

                vector
            < string, allocator > texts; 

                vector
            <textwords, allocator>::iterator iter = pvec->begin();
                
            for ( ; iter != pvec->end(); ++iter )
                          copy( (
            *iter).begin(), (*iter).end(), back_inserter( texts ));

                
            // sort the elements of texts
                sort( texts.begin(), texts.end() );
                for_each( texts.begin(), texts.end(), PrintElem
            <string>() );

                cout 
            << endl << endl;

                
            // delete all duplicate elements 
                vector<string, allocator>::iterator it;
                it 
            = unique( texts.begin(), texts.end() );
                texts.erase( it, texts.end() );
                for_each( texts.begin(), texts.end(), PrintElem
            <string>() );

                cout 
            << endl << endl;

                stable_sort( texts.begin(), texts.end(), LessThan() );
                for_each( texts.begin(), texts.end(), PrintElem
            <string>() );

                cout 
            << endl << endl;

                
            // count number of strings greater than length 6
                int cnt = 0;

                
            // obsolete form of count -- standard changes this
                count_if( texts.begin(), texts.end(), GreaterThan(), cnt );

                cout 
            << "Number of words greater than length six are "
                         
            << cnt << endl;

                
            // 

                
            static string rw[] = { "and""if""or""but""the" };
                vector
            <string,allocator> remove_words( rw, rw+5 );

                vector
            <string, allocator>::iterator it2 = remove_words.begin();
                
            for ( ; it2 != remove_words.end(); ++it2 ) {
                    
            int cnt = 0;
                    
            // obsolete form of count -- standard changes this
                        count( texts.begin(), texts.end(), *it2, cnt );
                
                    cout 
            << cnt << " instances removed:  " 
                             
            << (*it2) << endl;
                
                        texts.erase(
                            remove(texts.begin(),texts.end(),
            *it2),
                            texts.end()
                       );
                   }

                cout 
            << endl << endl;
                for_each( texts.begin(), texts.end(), PrintElem
            <string>() );
            }

            typedef vector
            <string,allocator>::difference_type diff_type;
            #include 
            <fstream.h>

            main()
            {
                 vector
            <textwords, allocator> sample;
                 vector
            <string,allocator>       t1, t2; 
                 
            string               t1fn, t2fn;

                 cout 
            << "text file #1: "; cin >> t1fn;
                 cout 
            << "text file #2: "; cin >> t2fn;

                 ifstream infile1( t1fn.c_str());
                 ifstream infile2( t2fn.c_str());

                 istream_iterator
            < string, diff_type > input_set1( infile1 ), eos; 
                 istream_iterator
            < string, diff_type > input_set2( infile2 );

                 copy( input_set1, eos, back_inserter( t1 ));
                 copy( input_set2, eos, back_inserter( t2 ));

                 sample.push_back( t1 ); sample.push_back( t2 );
                 process_vocab( 
            &sample );
            }

            posted @ 2009-12-21 00:14 蜜蜂 閱讀(207) | 評(píng)論 (0)編輯 收藏

            僅列出標(biāo)題  
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久受www免费人成| 亚洲国产精品无码久久98| 免费精品国产日韩热久久| 久久久精品人妻一区二区三区蜜桃| 精品精品国产自在久久高清 | 国产三级久久久精品麻豆三级 | 久久久久亚洲AV成人片| 久久久久无码国产精品不卡| 漂亮人妻被中出中文字幕久久| 91精品国产综合久久精品| 亚洲精品无码久久久久去q| 久久人人爽人人爽人人片av麻烦| 国产综合精品久久亚洲| 久久精品国产亚洲AV不卡| 99久久免费国产特黄| 亚洲国产精品一区二区久久hs| 精品久久久久久无码人妻热| 亚洲AV无码久久精品狠狠爱浪潮| 亚洲人成网站999久久久综合| 精品多毛少妇人妻AV免费久久| 狠狠久久亚洲欧美专区| 国产精品久久影院| 国产成人精品综合久久久| 国产精品99久久久久久董美香| 91精品日韩人妻无码久久不卡| 久久免费高清视频| 无码任你躁久久久久久| 国产一区二区久久久| 97精品依人久久久大香线蕉97| 成人午夜精品无码区久久| 亚洲精品无码久久久久去q| 日产精品久久久久久久性色| 久久精品免费全国观看国产| 久久精品一区二区三区中文字幕| 亚洲Av无码国产情品久久| 久久精品国产亚洲AV忘忧草18| 国产偷久久久精品专区| 91久久精品91久久性色| 国产精品美女久久久久AV福利 | 97精品依人久久久大香线蕉97| 久久AV高清无码|