• <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>
            隨筆 - 40, 文章 - 0, 評(píng)論 - 9, 引用 - 0
            數(shù)據(jù)加載中……

            STL vector的使用

              http://www.cppreference.com/cppvector/index.html
            assign
            Syntax:
              #include <vector>
            void assign( size_type num, const TYPE& val );
            void assign( input_iterator start, input_iterator end );
            

            The assign() function either gives the current vector the values from start to end, or gives it num copies of val.

            This function will destroy the previous contents of the vector.

            For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:

            vector<int> v;
             v
            .assign( 10, 42 );
             
            for( int i = 0; i < v.size(); i++ ) {
               cout
            << v[i] << " ";
             
            }
             cout
            << endl;            

            The above code displays the following output:

            42 42 42 42 42 42 42 42 42 42          

            The next example shows how assign() can be used to copy one vector to another:

            vector<int> v1;
             
            for( int i = 0; i < 10; i++ ) {
               v1
            .push_back( i );
             
            }              

             vector
            <int> v2;
             v2
            .assign( v1.begin(), v1.end() );            

             
            for( int i = 0; i < v2.size(); i++ ) {
               cout
            << v2[i] << " ";
             
            }
             cout
            << endl;            

            When run, the above code displays the following output:

            0 1 2 3 4 5 6 7 8 9     



            Vector constructors
            Syntax:
              #include <vector>
            vector();
            vector( const vector& c );
            vector( size_type num, const TYPE& val = TYPE() );
            vector( input_iterator start, input_iterator end );
            ~vector();
            

            The default vector constructor takes no arguments, creates a new instance of that vector.

            The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector c.

            The third constructor creates a vector with space for num objects. If val is specified, each of those objects will be given that value. For example, the following code creates a vector consisting of five copies of the integer 42:

            vector<int> v1( 5, 42 );         

            The last constructor creates a vector that is initialized to contain the elements between start and end. For example:

            // create a vector of random integers
             cout
            << "original vector: ";
             vector
            <int> v;
             
            for( int i = 0; i < 10; i++ ) {
               
            int num = (int) rand() % 10;
               cout
            << num << " ";
               v
            .push_back( num );
             
            }
             cout
            << endl;            

             
            // find the first element of v that is even
             vector
            <int>::iterator iter1 = v.begin();
             
            while( iter1 != v.end() && *iter1 % 2 != 0 ) {
               iter1
            ++;
             
            }              

             
            // find the last element of v that is even
             vector
            <int>::iterator iter2 = v.end();
             
            do {
               iter2
            --;
             
            } while( iter2 != v.begin() && *iter2 % 2 != 0 );              

             
            // only proceed if we find both numbers
             
            if( iter1 != v.end() && iter2 != v.begin() ) {
               cout
            << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl;        

               cout
            << "new vector: ";
               vector
            <int> v2( iter1, iter2 );
               
            for( int i = 0; i < v2.size(); i++ ) {
                 cout
            << v2[i] << " ";
               
            }
               cout
            << endl;
             
            }

            When run, this code displays the following output:

            original vector: 1 9 7 9 2 7 2 1 9 8
             first even number
            : 2, last even number: 8
             
            new vector: 2 7 2 1 9          

            All of these constructors run in linear time except the first, which runs in constant time.

            The default destructor is called when the vector should be destroyed.

             

            posted on 2007-12-11 14:26 閱讀(4040) 評(píng)論(2)  編輯 收藏 引用 所屬分類: c++開(kāi)發(fā)具體技術(shù)

            評(píng)論

            # re: STL vector的使用  回復(fù)  更多評(píng)論   

            我用的是VS2005 環(huán)境.
            如:for( int i = 0; i < v2.size(); i++ )
            系統(tǒng)提示 int 和 size_type 轉(zhuǎn)換 會(huì)丟失數(shù)據(jù).

            望看到回復(fù) hanbo79@sogou.com
            2008-08-16 11:25 | hb

            # re: STL vector的使用  回復(fù)  更多評(píng)論   

            size_type是unsigned類型比int大多了,可能被截?cái)啵阅鉯聲明為unsigned就可以了@hb
            2008-08-16 12:20 | 沈臻豪(foxtail)
            精品伊人久久大线蕉色首页| 久久九色综合九色99伊人| 亚洲中文字幕久久精品无码APP| 久久亚洲私人国产精品vA| 久久精品无码一区二区无码| 品成人欧美大片久久国产欧美...| 伊人久久大香线蕉无码麻豆| 亚洲精品乱码久久久久久| 国产AⅤ精品一区二区三区久久| 无码精品久久一区二区三区| 狠狠色婷婷久久一区二区三区| 久久国产高清一区二区三区| 久久SE精品一区二区| 国产精品日韩欧美久久综合| 久久久久高潮综合影院| 久久精品国产99久久丝袜| 久久精品国产亚洲av水果派| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 一本色道久久HEZYO无码| 成人午夜精品久久久久久久小说| 亚洲精品乱码久久久久久| 亚洲v国产v天堂a无码久久| 99久久综合狠狠综合久久止| 久久综合亚洲色一区二区三区| 久久婷婷国产麻豆91天堂| 日韩人妻无码精品久久免费一| 精品久久久久久无码不卡| 久久精品无码一区二区三区日韩| 久久成人精品视频| 久久大香香蕉国产| 色欲av伊人久久大香线蕉影院| 无码八A片人妻少妇久久| 久久精品国产精品亚洲下载| 国产国产成人久久精品| 狠狠色婷婷综合天天久久丁香| 久久精品中文无码资源站| 2021国内久久精品| 性欧美丰满熟妇XXXX性久久久 | 国产美女亚洲精品久久久综合| 国产精品伊人久久伊人电影 | 久久天天躁狠狠躁夜夜2020一 |