<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          C++ STL的概念及舉例

          作者: 時(shí)間:2011-09-06 來源:網(wǎng)絡(luò) 收藏

            本篇文章是作者本人使用 后的一些看法, 對(duì)於想要靠此文章學(xué)習(xí), 是不可能的. 建議叁后面介紹的一些書入門.

            的概念

            在STL 中, 大至上分三個(gè)主要的功能. 分別是collect class, (例如 vector ,list , .....), 算法部份(如sort ,find ...), 最是一些工具(如auto_ptr , make_pair ....), 其中以collect class 和算法部份最為重要.

            STL 使用一種稱作 iterator (中文有人翻作泛型指標(biāo)) 的資料類型來連接collect class 和算法部份. 為了達(dá)到能夠重復(fù)使用算法和collect class. 在STL 中把這些東西更進(jìn)一步抽象化. 因此STL訂義了一堆抽象化的行為稱作concept. 您可以想像concept 是在說明一些行為. 只要某一個(gè)型別合乎concept 中說明的行為. 就說此類型具有此concept.

            舉例說明, 如有concept C1,C2. 算法A1, A2. 資料類型D1 具有C1 的行為,D2 有C1,C2.

            如A1 要求需要具有C1 的型別. 則任何一個(gè)有C1 的資料類型都可以給A1 使用. 在此例中有D1和D2.

            如A2 要求具有C1跟C2的型別. 在此只有D2 可以給A2 使用.

            STL 使用抽象化的行為定義且實(shí)作它們, 來達(dá)到算法和資料類型的重復(fù)使用.

            iterator 在STL 中是很重要的一個(gè)概念, iterator 和C 的point 很相, 但他不是指標(biāo)(note 在某一些狀況下iterator 是以C 的point 來實(shí)作). 他提供類似point 的功能, 如 '++' 運(yùn)算表示把iterator 移到下一個(gè)element , '*' 運(yùn)算表示存取iterator 目前指向的element (在此使用'指向'并不是很好). 利用iterator 你可以走訪整個(gè)collect 中存放的內(nèi)容.

            舉個(gè)例子說明iteraotr 的功能吧.

            vector 是STL 中眾多的collect class 中的一種. vector 提供begin() 和end() 這類的member function 來取得vector 中第一個(gè)element 的iterator, 和最后一個(gè)element的下一個(gè)element. 您可以這樣使用:

            std::vector vtInt;

            vtInt.push_back(1);

            ...

            .. // 假設(shè)對(duì)vtInt 做了很多不同的動(dòng)做, 如push_back ...

            std::vector::iterator itBegin=vtInt.begin();

            std::vector::iterator itEnd=vtInt.end();

            for( ; itBegin != itEnd ;++itBegin) // 走訪itBegin 到 itEnd 不含itEnd 之間的所有element

            {

            int x=*itBegin; // 取得itBegin 指向的資料

            *itBegin = .... ; // 存放資料到itBegin 指向的資料

            }

            由此例中iterator 有和C point 類似的功能.

            機(jī)乎所有的STL 的算法都會(huì)要求輸入的資料是itearot 如sort 算法, 要求輸入兩個(gè)iterator , 然后把這兩個(gè)iterator 之間的資料加以排序. 介由引進(jìn)iterator , sort 可以排序的東西就變多了.

            使用范例

            I. 在STL 中使用標(biāo)準(zhǔn)輸出. Say Hello

            #include // STL 的輸入輸出包含檔. 在STL 中都沒有使用.h or .hpp 等副檔名

            int main(int argc,char* argv[])

            {

            std::cout "Hello!! This is STL " std::endl; // 使用stl 的標(biāo)準(zhǔn)輸出, std 是STL 的namespace 所以要這樣用std::cout

            return 0;

            }

            II. 從標(biāo)準(zhǔn)輸入讀入字串, 排序后再輸出

            #include // STL 的輸入輸出包含檔

            #include // STL 的算法包含檔

            #include // STL 的字串

            #include // STL 的一個(gè)collect class 這是一個(gè)像陣列的class

            const int d_nReadLine=5;

            int main(int argc,char* argv[])

            {

            std::vector vtString; // 宣告一個(gè)字串vector

            std::string strTemp; // 宣告一個(gè)字串

            for( int i = 0 ; i d_nReadLine ; i++) // 讀取d_nReadLine 個(gè)字串并將結(jié)果存到vtString 中

            {

            std::cin >> strTemp; // 讀入字串

            vtString.push_back(strTemp); // 將讀入的字串存到vtString 的最后面

            }

            std::sort(vtString.begin(),vtString.end()); // 將vtString 中得資料sort

            std::copy(vtString.begin(),vtString.end(),

            std::ostream_iterator(std::cout," ")); // 將vtString 中得資料輸出

            return 0;

            }

            note :

            std::sort 和std::copy 都是STL 提供的算法.

          c++相關(guān)文章:c++教程




          關(guān)鍵詞: C++ STL

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();