• vector 学习


    #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;
    }
    

      

  • 相关阅读:
    ASP.NET Web API 控制器执行过程(一)
    ASP.NET Web API 控制器创建过程(二)
    ASP.NET Web API 控制器创建过程(一)
    ASP.NET Web API WebHost宿主环境中管道、路由
    ASP.NET Web API Selfhost宿主环境中管道、路由
    ASP.NET Web API 管道模型
    ASP.NET Web API 路由对象介绍
    ASP.NET Web API 开篇示例介绍
    ASP.NET MVC 视图(五)
    ASP.NET MVC 视图(四)
  • 原文地址:https://www.cnblogs.com/kljfdsa/p/7748124.html
Copyright © 2020-2023  润新知