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

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

            const int GamePlayer::NumTurns;



            一個class需要常量值,定義如下:

            enum{NumTurns = 5};

            int scores[NumTurns];



            關于“形似函數的宏,最好改用inline函數替換#defines”

            但,經驗證明。宏的速度要比用inline的方法快。

            在一些對速度要求非常嚴格的項目中,這個方面還是要有所取舍的。

            posted @ 2009-12-21 00:18 蜜蜂 閱讀(233) | 評論 (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 蜜蜂 閱讀(205) | 評論 (0)編輯 收藏

            僅列出標題  
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产99久久久香蕉| 亚洲精品国产字幕久久不卡| 午夜不卡888久久| 亚洲欧美精品伊人久久| 久久久久亚洲精品天堂久久久久久| 精品久久人人爽天天玩人人妻| 欧美精品福利视频一区二区三区久久久精品| 合区精品久久久中文字幕一区| 色综合久久久久久久久五月| 亚洲国产成人久久综合碰碰动漫3d | 亚洲AV日韩精品久久久久| 久久人人爽人人爽人人AV东京热| 国产精品99久久久久久猫咪| 亚洲综合熟女久久久30p| 亚洲国产天堂久久综合网站| 亚洲AV日韩精品久久久久久 | 国产精品99久久久久久宅男小说| 亚洲第一极品精品无码久久| 久久精品?ⅴ无码中文字幕| 久久婷婷五月综合色奶水99啪| 久久精品国产精品青草app| 色综合久久久久无码专区 | 欧美午夜A∨大片久久| 91久久精品91久久性色| 久久精品中文字幕一区| 久久人人超碰精品CAOPOREN| 久久精品国产亚洲网站| 久久不见久久见免费视频7| 中文字幕热久久久久久久| 久久人人爽人爽人人爽av| 国产精品美女久久久免费| 久久99毛片免费观看不卡| 精品久久一区二区三区| 国产精品久久久久久一区二区三区| 精品国产乱码久久久久软件| 欧美日韩精品久久久久| 亚洲国产天堂久久久久久| 欧美激情精品久久久久久久九九九| 国产精品免费久久久久电影网| 99久久国产综合精品网成人影院 | 女人高潮久久久叫人喷水|