• <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>
            posts - 29,comments - 10,trackbacks - 0
            標準模板庫(The Standard Template Library, STL)定義了五種迭代器。下面的圖表畫出了這幾種:

                        input         output
                          \            /
                             forward
                                 |
                            bidirectional
                                 |
                           random access

             

             

            要注意,上面這圖表并不是表明它們之間的繼承關系:而只是描述了迭代器的種類和接口。處于圖表下層的迭代器都是相對于處于圖表上層迭代器的擴張集。例如:forward迭代器不但擁有inputoutput迭代器的所有功能,還擁有更多的功能。

            各個迭代器的功能如下:

            迭代器類別

            說明

            輸入

            從容器中讀取元素。輸入迭代器只能一次讀入一個元素向前移動,輸入迭代器只支持一遍算法,同一個輸入迭代器不能兩遍遍歷一個序列

            輸出

            向容器中寫入元素。輸出迭代器只能一次一個元素向前移動。輸出迭代器只支持一遍算法,統一輸出迭代器不能兩次遍歷一個序列

            正向

            組合輸入迭代器和輸出迭代器的功能,并保留在容器中的位置

            雙向

            組合正向迭代器和逆向迭代器的功能,支持多遍算法

            隨機訪問

            組合雙向迭代器的功能與直接訪問容器中任何元素的功能,即可向前向后跳過任意個元素

            迭代器的操作:

            每種迭代器均可進行包括表中前一種迭代器可進行的操作。

            迭代器操作

            說明

            所有迭代器

            p++

            后置自增迭代器

            ++p

            前置自增迭代器

            輸入迭代器

            *p

            復引用迭代器,作為右值

            p=p1

            將一個迭代器賦給另一個迭代器

            p==p1

            比較迭代器的相等性

            p!=p1

            比較迭代器的不等性

            輸出迭代器

            *p

            復引用迭代器,作為左值

            p=p1

            將一個迭代器賦給另一個迭代器

            正向迭代器

            提供輸入輸出迭代器的所有功能

            雙向迭代器

            --p

            前置自減迭代器

            p--

            后置自減迭代器

            隨機迭代器

            p+=i

            將迭代器遞增i

            p-=i

            將迭代器遞減i

            p+i

            p位加i位后的迭代器

            p-i

            p位減i位后的迭代器

            p[i]

            返回p位元素偏離i位的元素引用

            p<p1

            如果迭代器p的位置在p1前,返回true,否則返回false

            p<=p1

            p的位置在p1的前面或同一位置時返回true,否則返回false

            p>p1

            如果迭代器p的位置在p1后,返回true,否則返回false

            p>=p1

            p的位置在p1的后面或同一位置時返回true,否則返回false

            只有順序容器和關聯容器支持迭代器遍歷,各容器支持的迭代器的類別如下:

            容器

            支持的迭代器類別

            說明

            vector

            隨機訪問

            一種隨機訪問的數組類型,提供了對數組元素進行快速隨機訪問以及在序列尾部進行快速的插入和刪除操作的功能。可以再需要的時候修改其自身的大小

            deque

            隨機訪問

            一種隨機訪問的數組類型,提供了序列兩端快速進行插入和刪除操作的功能。可以再需要的時候修改其自身的大小

            list

            雙向

            一種不支持隨機訪問的數組類型,插入和刪除所花費的時間是固定的,與位置無關。

            set

            雙向

            一種隨機存取的容器,其關鍵字和數據元素是同一個值。所有元素都必須具有惟一值。

            multiset

            雙向

            一種隨機存取的容器,其關鍵字和數據元素是同一個值。可以包含重復的元素。

            map

            雙向

            一種包含成對數值的容器,一個值是實際數據值,另一個是用來尋找數據的關鍵字。一個特定的關鍵字只能與一個元素關聯。

            multimap

            雙向

            一種包含成對數值的容器,一個值是實際數據值,另一個是用來尋找數據的關鍵字。一個關鍵字可以與多個數據元素關聯。

            stack

            不支持

            適配器容器類型,用vectordequelist對象創建了一個先進后出容器

            queue

            不支持

            適配器容器類型,用dequelist對象創建了一個先進先出容器

            priority_queue

            不支持

            適配器容器類型,用vectordeque對象創建了一個排序隊列

            下面列舉了些例子說明各個容器的用法:
            1、vector
            #include <iostream>
            #include 
            <vector>

            int main()
            {
                std::vector
            <char> charVector;

                
            int x;
                
            for (x=0; x<10++x)
                    charVector.push_back(
            65 + x);

                
            int size = charVector.size();
                
            for (x=0; x<size; ++x)
                {
                    std::vector
            <char>::iterator start =
                        charVector.begin();
                    charVector.erase(start);
                    std::vector
            <char>::iterator iter;
                    
            for (iter = charVector.begin(); 
                            iter 
            != charVector.end(); iter++)
                    {
                        std::cout 
            << *iter;
                    }
                    std::cout 
            << std::endl;
                }

                
            return 0;
            }
            2、deque
            #include <iostream>
            #include 
            <deque>

            int main()
            {
                std::deque
            <char> charDeque;
                
            int x;
                
            for (x=0; x<10++x)
                    charDeque.push_front(
            65 + x);

                
            int size = charDeque.size();
                
            for (x=0; x<size; ++x)
                {
                    std::deque
            <char>::iterator start =
                        charDeque.begin();
                    charDeque.erase(start);
                    std::deque
            <char>::iterator iter;
                    
            for (iter = charDeque.begin(); 
                            iter 
            != charDeque.end(); iter++)
                    {
                        std::cout 
            << *iter;
                    }
                    std::cout 
            << std::endl;
                }

                
            return 0;
            }
            3、list
            #include <iostream>
            #include 
            <list>

            int main()
            {
                
            // Create and populate the list.
                int x;
                std::list
            <char> charList;
                
            for (x=0; x<10++x)
                    charList.push_front(
            65 + x);

                
            // Display contents of list.
                std::cout << "Original list: ";
                std::list
            <char>::iterator iter;
                
            for (iter = charList.begin(); 
                        iter 
            != charList.end(); iter++)
                {
                    std::cout 
            << *iter;
                    
            //char ch = *iter;
                    
            //std::cout << ch;
                }
                std::cout 
            << std::endl;
                
                
            // Insert five Xs into the list.
                std::list<char>::iterator start = charList.begin();
                charList.insert(
            ++start, 5'X');

                
            // Display the result.
                std::cout << "Resultant list: ";
                
            for (iter = charList.begin(); 
                iter 
            != charList.end(); iter++)
                {
                    std::cout 
            << *iter;
                    
            //char ch = *iter;
                    
            //std::cout << ch;
                }
                
                
            return 0;
            }
            4、set
            #include <iostream>
            #include 
            <set>

            int main()
            {
                
            // Create the set object.
                std::set<char> charSet;

                
            // Populate the set with values.
                charSet.insert('E');
                charSet.insert(
            'D');
                charSet.insert(
            'C');
                charSet.insert(
            'B');
                charSet.insert(
            'A');

                
            // Display the contents of the set.
                std::cout << "Contents of set: " << std::endl;
                std::
            set<char>::iterator iter;
                
            for (iter = charSet.begin(); iter != charSet.end(); iter++)
                    std::cout 
            << *iter << std::endl;
                std::cout 
            << std::endl;

                
            // Find the D.
                iter = charSet.find('D');
                
            if (iter == charSet.end())
                    std::cout 
            << "Element not found.";
                
            else
                    std::cout 
            << "Element found: " << *iter;

                
            return 0;
            }
            5、multiset
            #include <iostream>
            #include 
            <set>

            int main()
            {
                
            // Create the first set object.
                std::multiset<char> charMultiset1;

                
            // Populate the multiset with values.
                charMultiset1.insert('E');
                charMultiset1.insert(
            'D');
                charMultiset1.insert(
            'C');
                charMultiset1.insert(
            'B');
                charMultiset1.insert(
            'A');
                charMultiset1.insert(
            'B');
                charMultiset1.insert(
            'D');

                
            // Display the contents of the first multiset.
                std::cout << "Contents of first multiset: " << std::endl;
                std::multiset
            <char>::iterator iter;
                
            for (iter = charMultiset1.begin();
                        iter 
            != charMultiset1.end(); iter++)
                    std::cout 
            << *iter << std::endl;
                std::cout 
            << std::endl;

                
            // Create the second multiset object.
                std::multiset<char> charMultiset2;

                
            // Populate the multiset with values.
                charMultiset2.insert('J');
                charMultiset2.insert(
            'I');
                charMultiset2.insert(
            'H');
                charMultiset2.insert(
            'G');
                charMultiset2.insert(
            'F');
                charMultiset2.insert(
            'G');
                charMultiset2.insert(
            'I');
                
                
            // Display the contents of the second multiset.
                std::cout << "Contents of second multiset: "
                    
            << std::endl;
                
            for (iter = charMultiset2.begin();
                iter 
            != charMultiset2.end(); iter++)
                    std::cout 
            << *iter << std::endl;
                std::cout 
            << std::endl;
                
                
            // Compare the sets.
                if (charMultiset1 == charMultiset2)
                    std::cout 
            << "set1 == set2";
                
            else if (charMultiset1 < charMultiset2)
                    std::cout 
            << "set1 < set2";
                
            else if (charMultiset1 > charMultiset2)
                    std::cout 
            << "set1 > set2";
                
                
            return 0;
            }
            6、map
            #include <iostream>
            #include 
            <map>

            typedef std::map
            <intchar> MYMAP;

            int main()
            {
                
            // Create the first map object.
                MYMAP charMap1;

                
            // Populate the first map with values.
                charMap1[1= 'A';
                charMap1[
            4= 'D';
                charMap1[
            2= 'B';
                charMap1[
            5= 'E';
                charMap1[
            3= 'C';

                
            // Display the contents of the first map.
                std::cout << "Contents of first map: " << std::endl;
                MYMAP::iterator iter;
                
            for (iter = charMap1.begin();
                        iter 
            != charMap1.end(); iter++)
                {
                    std::cout 
            << (*iter).first << " --> ";
                    std::cout 
            << (*iter).second << std::endl;
                }
                std::cout 
            << std::endl;

                
            // Create the second map object.
                MYMAP charMap2;

                
            // Populate the first map with values.
                charMap2[1= 'F';
                charMap2[
            4= 'I';
                charMap2[
            2= 'G';
                charMap2[
            5= 'J';
                charMap2[
            3= 'H';

                
            // Display the contents of the second map.
                std::cout << "Contents of second map: " << std::endl;
                
            for (iter = charMap2.begin();
                        iter 
            != charMap2.end(); iter++)
                {
                    std::cout 
            << (*iter).first << " --> ";
                    std::cout 
            << (*iter).second << std::endl;
                }
                std::cout 
            << std::endl;

                
            // Compare the maps.
                if (charMap1 == charMap2)
                    std::cout 
            << "map1 == map2";
                
            else if (charMap1 < charMap2)
                    std::cout 
            << "map1 < map2";
                
            else if (charMap1 > charMap2)
                    std::cout 
            << "map1 > map2";
                
                
            return 0;
            }
            7、multimap
            #include <iostream>
            #include 
            <map>

            typedef std::multimap
            <intchar> MYMAP;

            int main()
            {
                
            // Create the first multimap object.
                MYMAP charMultimap;

                
            // Populate the multimap with values.
                charMultimap.insert(MYMAP::value_type(1,'A'));
                charMultimap.insert(MYMAP::value_type(
            4,'C'));
                charMultimap.insert(MYMAP::value_type(
            2,'B'));
                charMultimap.insert(MYMAP::value_type(
            7,'E'));
                charMultimap.insert(MYMAP::value_type(
            5,'D'));
                charMultimap.insert(MYMAP::value_type(
            3,'B'));
                charMultimap.insert(MYMAP::value_type(
            6,'D'));

                
            // Display the contents of the first multimap.
                std::cout << "Contents of first multimap: " << std::endl;
                MYMAP::iterator iter;
                
            for (iter = charMultimap.begin();
                        iter 
            != charMultimap.end(); iter++)
                {
                    std::cout 
            << (*iter).first << " --> ";
                    std::cout 
            << (*iter).second << std::endl;
                }
                std::cout 
            << std::endl;

                
            // Create the second multimap object.
                MYMAP charMultimap2;

                
            // Populate the second multimap with values.
                charMultimap2.insert(MYMAP::value_type(1,'C'));
                charMultimap2.insert(MYMAP::value_type(
            4,'F'));
                charMultimap2.insert(MYMAP::value_type(
            2,'D'));
                charMultimap2.insert(MYMAP::value_type(
            7,'E'));
                charMultimap2.insert(MYMAP::value_type(
            5,'F'));
                charMultimap2.insert(MYMAP::value_type(
            3,'E'));
                charMultimap2.insert(MYMAP::value_type(
            6,'G'));

                
            // Display the contents of the second multimap.
                std::cout << "Contents of second multimap: " << std::endl;
                
            for (iter = charMultimap2.begin();
                        iter 
            != charMultimap2.end(); iter++)
                {
                    std::cout 
            << (*iter).first << " --> ";
                    std::cout 
            << (*iter).second << std::endl;
                }
                std::cout 
            << std::endl;

                
            // Compare the multimaps.
                if (charMultimap == charMultimap2)
                    std::cout 
            << "multimap1 == multimap2";
                
            else if (charMultimap < charMultimap2)
                    std::cout 
            << "multimap1 < multimap2";
                
            else if (charMultimap > charMultimap2)
                    std::cout 
            << "multimap1 > multimap2";
                
                
            return 0;
            }
            8、stack
            #include <iostream>
            #include 
            <list>
            #include 
            <stack>

            int main()
            {
                std::stack
            <int, std::list<int> > intStack;

                
            int x;
                std::cout 
            << "Values pushed onto stack:"
                          
            << std::endl;
                
            for (x=1; x<11++x)
                {
                    intStack.push(x
            *100);
                    std::cout 
            << x*100 << std::endl;
                }

                std::cout 
            << "Values popped from stack:"
                          
            << std::endl;
                
            int size = intStack.size();
                
            for (x=0; x<size; ++x)
                {
                    std::cout 
            << intStack.top() << std::endl;
                    intStack.pop();
                }

                
            return 0;
            }
            9、queue
            #include <iostream>
            #include 
            <list>
            #include 
            <queue>

            int main()
            {
                std::queue
            <int, std::list<int> > intQueue;

                
            int x;
                std::cout 
            << "Values pushed onto queue:"
                          
            << std::endl;
                
            for (x=1; x<11++x)
                {
                    intQueue.push(x
            *100);
                    std::cout 
            << x*100 << std::endl;
                }

                std::cout 
            << "Values removed from queue:"
                          
            << std::endl;
                
            int size = intQueue.size();
                
            for (x=0; x<size; ++x)
                {
                    std::cout 
            << intQueue.front() << std::endl;
                    intQueue.pop();
                }

                
            return 0;
            }
            10、priority_queue
            #include <iostream>
            #include 
            <list>
            #include 
            <queue>

            int main()
            {
                std::priority_queue
            <int, std::vector<int>,std::greater<int> > intPQueue;
                
            int x;
                intPQueue.push(
            400);
                intPQueue.push(
            100);
                intPQueue.push(
            500);
                intPQueue.push(
            300);
                intPQueue.push(
            200);

                std::cout 
            << "Values removed from priority queue:"
                          
            << std::endl;
                
            int size = intPQueue.size();
                
            for (x=0; x<size; ++x)
                {
                    std::cout 
            << intPQueue.top() << std::endl;
                    intPQueue.pop();
                }

                
            return 0;
            }
            posted on 2009-06-18 20:12 The_Moment 閱讀(7579) 評論(0)  編輯 收藏 引用 所屬分類: C\C++
            一本大道久久a久久精品综合| 久久精品国产男包| 欧洲成人午夜精品无码区久久| 久久精品国产欧美日韩| 国产精品99久久久久久www| 久久美女网站免费| 久久99热精品| 99久久精品久久久久久清纯| 国产精品美女久久久| 精品一区二区久久久久久久网站| 久久国产精品无码HDAV| 久久91亚洲人成电影网站| 国产精品免费福利久久| 国产精品毛片久久久久久久| 久久99热狠狠色精品一区| 品成人欧美大片久久国产欧美...| 久久久精品免费国产四虎| 国产综合免费精品久久久| 精品久久久无码中文字幕| 三级片免费观看久久| 久久久午夜精品| 久久99精品久久久久久动态图| 99精品国产在热久久| 久久精品国产只有精品66| 欧美国产精品久久高清| 亚洲精品乱码久久久久久久久久久久 | 一本久久a久久精品vr综合| 色诱久久久久综合网ywww| 91亚洲国产成人久久精品网址| 精品人妻伦九区久久AAA片69| 色老头网站久久网| 亚洲AV无码久久| 久久久久国产一区二区| 欧美熟妇另类久久久久久不卡| 亚洲国产成人久久综合碰碰动漫3d | 亚洲Av无码国产情品久久| 久久精品午夜一区二区福利| 国内精品伊人久久久久影院对白| 久久久久久久波多野结衣高潮| 狠狠色丁香久久婷婷综| 免费无码国产欧美久久18|