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

            T9的空間

            You will never walk alone!

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks

            菜菜的學(xué)習(xí)了一下STL的vector,做一下筆記感覺這個網(wǎng)站講得通俗易懂http://www.cppreference.com/cppvector/index.html 寫了一下測試code(待續(xù))

            C++ Vectors

            Vectors contain contiguous elements stored as an array. Accessing members of a vector or appending elements can be done in constant time, whereas locating a specific value or inserting elements into the vector takes linear time.

            Display all entries for C++ Vectors on one page, or view entries individually:

            Vector constructors create vectors and initialize them with some data
            Vector operators compare, assign, and access elements of a vector
            assign assign elements to a vector
            at returns an element at a specific location
            back returns a reference to last element of a vector
            begin returns an iterator to the beginning of the vector
            capacity returns the number of elements that the vector can hold
            clear removes all elements from the vector
            empty true if the vector has no elements
            end returns an iterator just past the last element of a vector
            erase removes elements from a vector
            front returns a reference to the first element of a vector
            insert inserts elements into the vector
            max_size returns the maximum number of elements that the vector can hold
            pop_back removes the last element of a vector
            push_back add an element to the end of the vector
            rbegin returns a reverse_iterator to the end of the vector
            rend returns a reverse_iterator to the beginning of the vector
            reserve sets the minimum capacity of the vector
            resize change the size of the vector
            size returns the number of items in the vector
            swap swap the contents of this vector with another

             1#include<vector>
             2#include<iostream>
             3#include<algorithm>
             4using namespace std;
             5void print(int a){cout<<a<<" ";}
             6int main()
             7{
             8    vector<int>v,v1;
             9    vector<int>::iterator iv1,iv2;
            10    vector<int>v2;
            11    v2.reserve(10);    
            12    v.assign(10,2);
            13    cout<<v2.capacity()<<endl;//capacity容量輸出為10
            14    cout<<v2.size()<<endl;//大小輸出為1
            15    int i;
            16    for(i=0;i<7;i++)v2.push_back(i);
            17    for_each(v2.begin(),v2.end(),print);
            18    cout<<endl;
            19    for(iv1=v2.begin();iv1!=v2.end();iv1++)
            20        if(*iv1==3)break;
            21    if(iv1!=v2.end())v2.insert(iv1,99);
            22    for_each(v2.begin(),v2.end(),print);
            23    cout<<endl;
            24    if(iv1!=v2.end())v2.insert(iv1,5,99);
            25    for_each(v2.begin(),v2.end(),print);
            26    cout<<endl;
            27    if(iv1!=v2.end())v2.insert(iv1,v.begin(),v.end());
            28    for_each(v2.begin(),v2.end(),print);
            29    cout<<endl;
            30    cout<<v2.size()<<endl;
            31    v2.erase(v2.begin());
            32    cout<<v2.size()<<endl;
            33    v2.erase(v2.begin(),v2.end());
            34    for_each(v2.begin(),v2.end(),print);
            35    iv1=v.begin();iv2=v.end();
            36    cout<<v.begin()<<endl;
            37    cout<<v.end()<<endl;
            38    cout<<iv1<<endl;
            39    cout<<iv2<<endl;
            40//    for(i=0;i<15;i++)cout<<v[i]<<" ";
            41//    cout<<endl;
            42//    for(i=0;i<15;i++)cout<<v.at(i)<<" ";//at()函數(shù)更加安全;
            43//    cout<<endl;
            44    for_each(v.begin(),v.end(),print);
            45    cout<<endl;
            46//    v1.assign(v.begin(),v.end());
            47//    v1.assign(iv1+2,iv2-2);
            48//    for(i=0;i<10;i++)cout<<v1[i]<<' ';
            49//    cout<<endl;
            50    for(i=0;i<10;i++)v1.push_back(i);
            51    for_each(v1.begin()+1,v1.end()-2,print);//輸出begin開始指向的數(shù)據(jù),直到end指向的前一個數(shù)據(jù)
            52    for_each(v1.begin(),v1.end(),print);
            53    cout<<endl;
            54    cout<<v1.back()<<endl;
            55    cout<<v1.front()<<endl;
            56    cout<<v1.size()<<endl;
            57    v.clear();
            58    cout<<v.size()<<endl;
            59    for_each(v.begin(),v.end(),print);//已經(jīng)clear,begin==end,不會有任何結(jié)果。
            60    return 0;
            61}

            62
            63
            64
            65//試了一下結(jié)構(gòu)體,感覺自己簡直太水了,努力學(xué)習(xí)中!
            66
            67#include<iostream>
            68#include<vector>
            69using namespace std;
            70typedef struct node{
            71    int x;
            72    int y;
            73}
            node;
            74ostream operator<<(ostream in,node a){
            75    in<<a.x<<a.y<<endl;
            76    return in;
            77}

            78int main()
            79{
            80    vector<node>v(10);
            81    v[0].x=1;v[0].y=1;
            82    vector<node>::iterator iv(v.begin());
            83    cout<<v[0].x<<v[0].y<<endl;
            84    cout<<*iv<<endl;
            85    cout<<iv->x<<iv->y<<endl;
            86    return 0;
            87}
            posted on 2008-08-13 00:54 Torres 閱讀(170) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            99久久精品免费国产大片| 亚洲午夜精品久久久久久app| 99久久无色码中文字幕人妻| 亚洲狠狠婷婷综合久久蜜芽| 国产精品久久久久9999高清| 久久久久久久久久久免费精品| 要久久爱在线免费观看| 久久综合噜噜激激的五月天| 久久国产精品-久久精品| 内射无码专区久久亚洲| 国产成人精品久久二区二区| 亚洲日本va午夜中文字幕久久| 久久久久亚洲av无码专区喷水 | 一本一道久久a久久精品综合| 一本色综合网久久| 国产精品嫩草影院久久| 久久人人爽人人爽人人AV| 人妻无码久久精品| 国产精品一久久香蕉国产线看观看| 26uuu久久五月天| 久久久精品国产免大香伊 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久91精品国产91久久麻豆| 亚洲国产高清精品线久久 | 亚洲va中文字幕无码久久| 久久久久亚洲精品中文字幕| 狠狠色丁香久久婷婷综| 久久夜色精品国产噜噜麻豆| 久久人妻AV中文字幕| 久久免费观看视频| 嫩草影院久久99| 国产精品女同久久久久电影院| 久久久久久精品免费看SSS | A级毛片无码久久精品免费| 97久久久久人妻精品专区 | 国产精品伦理久久久久久| 久久亚洲精品中文字幕三区| 丰满少妇人妻久久久久久| 日本人妻丰满熟妇久久久久久| 久久亚洲AV无码精品色午夜| 囯产极品美女高潮无套久久久|