vector 学习

本文详细介绍了使用C++标准库中的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;
}

  

转载于:https://www.cnblogs.com/kljfdsa/p/7748124.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值