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

            類(lèi)中定義常量: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 蜜蜂 閱讀(240) | 評(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 蜜蜂 閱讀(213) | 評(píng)論 (0)編輯 收藏

            僅列出標(biāo)題  
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類(lèi)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久久久伊人高潮影院| 久久99精品国产麻豆| 91精品国产91久久久久久青草| 日产精品久久久久久久| 狠狠色噜噜狠狠狠狠狠色综合久久 | 久久国产精品无码网站| 亚洲国产成人久久笫一页| 久久久久久精品无码人妻| 99麻豆久久久国产精品免费 | 中文成人久久久久影院免费观看| 热久久国产欧美一区二区精品| 色天使久久综合网天天| 日产精品99久久久久久| 狠狠色丁香婷婷综合久久来来去 | 狠狠久久亚洲欧美专区 | 亚洲另类欧美综合久久图片区| 久久精品黄AA片一区二区三区| 久久久久国产一区二区| 97久久天天综合色天天综合色hd| 久久久久久av无码免费看大片| 国内精品久久人妻互换| 大香伊人久久精品一区二区| 狠狠色丁香久久综合婷婷| 一本一本久久aa综合精品 | 久久人爽人人爽人人片AV | 久久亚洲国产最新网站| 国内精品免费久久影院| 久久精品国产亚洲麻豆| 久久精品国产网红主播| 久久精品人妻中文系列| 久久无码人妻精品一区二区三区 | 久久精品中文字幕有码| 国产L精品国产亚洲区久久| 国产情侣久久久久aⅴ免费| 亚洲AV日韩AV天堂久久| 超级碰碰碰碰97久久久久| 亚洲国产日韩欧美久久| 日韩久久无码免费毛片软件| 久久久久97国产精华液好用吗| 国产精品欧美久久久久无广告| 亚洲国产精品久久久久婷婷老年|