码迷,mamicode.com
首页 > 其他好文 > 详细

vector 学习

时间:2017-10-28 21:59:41      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ras   capacity   size   cap   algo   setw   last   学习   cout   

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

void printarray ( const vector <int> & );

int main() {
    vector <int> array1 = {3, 2, 7, 12, 3, 7, 10, 9, 89};
    vector <int> array2 ( array1 );

    cout << "---------------------------------------------------" << endl;
    cout << "the size of array1: " << array1.size() << endl;
    cout << "the capacity of array1: " << array1.capacity() << endl;
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.push_back ( 2 );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.pop_back();
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.insert ( array1.begin() + 5, 99 );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.insert ( array1.end() - 5, 85 );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.erase ( array1.end() - 5 );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    array1.erase ( array1.begin() + 2, array1.end() - 5 );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    cout << "the first element of array1: " << array1.front() << endl;
    cout << "the last element of array1:" << array1.back() << endl;
    cout << "max number element of array1:" << array1.max_size() << endl;

    cout << "---------------------------------------------------" << endl;
    array1.at ( 5 ) = 20;
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    reverse ( array1.begin(), array1.end() );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    sort ( array1.begin(), array1.end() );
    printarray ( array1 );

    cout << "---------------------------------------------------" << endl;
    printarray ( array1 );
    cout << "the size of array1: " << array1.size() << endl;
    cout << "the capacity of array1: " << array1.capacity() << endl;
    printarray ( array2 );
    cout << "the size of array2: " << array2.size() << endl;
    cout << "the capacity of array2: " << array2.capacity() << endl;

    array2.swap ( array1 );
    printarray ( array1 );
    cout << "the size of array1: " << array1.size() << endl;
    cout << "the capacity of array1: " << array1.capacity() << endl;
    printarray ( array2 );
    cout << "the size of array2: " << array2.size() << endl;
    cout << "the capacity of array2: " << array2.capacity() << endl;

    cout << "---------------------------------------------------" << endl;
    vector <int> (array2).swap(array2);
    printarray ( array2 );
    cout << "the size of array2: " << array2.size() << endl;
    cout << "the capacity of array2: " << array2.capacity() << endl;

    cout << "---------------------------------------------------" << endl;
    array2.clear();
    printarray ( array2 );
    cout << "the size of array2: " << array2.size() << endl;
    cout << "the capacity of array2: " << array2.capacity() << endl;

    return 0;
}

void printarray ( const vector <int> & myarray ) {
    for ( size_t i = 0; i < myarray.size(); i++ ) {
        cout << setw ( 5 ) << myarray[i] ;
        if ( 0 == ( i + 1 ) % 10 ) {
            cout << endl;
        }
    }
    cout << endl;
}

  

vector 学习

标签:ras   capacity   size   cap   algo   setw   last   学习   cout   

原文地址:http://www.cnblogs.com/tonyturtle/p/7748124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!