STL中Vector的使用方法详解

// VectorDemo.cpp : 定义控制台应用程序的入口点。
//Visual C++ 6.0 中编译通过


# include <iostream>
# include <vector>

using namespace std;

typedef vector<int> INTVECTOR;

void PrintIntVector(INTVECTOR iVector)
{
    //遍历vector方法
    cout<<"Vector: [";
    for(INTVECTOR::iterator it = iVector.begin(); it != iVector.end(); it++)
    {
        cout<<*it;
        if(it != iVector.end()-1)
        {
            cout<<",";
        }
    }
    cout<<"]"<<endl;
}
int main(int argc, char* argv[])
{
    INTVECTOR iVector;
    //push_back方法
    iVector.push_back(2);
    iVector.push_back(3);
    iVector.push_back(4);
    iVector.push_back(1);
    iVector.push_back(10);
    iVector.push_back(20);
    PrintIntVector(iVector);
    //Vector: [2,3,4,1,10,20]
   
    //pop_back方法
    iVector.pop_back();
    PrintIntVector(iVector);
    //Vector: [2,3,4,1,10]
   
    //size、capacity 与 max_size方法
    cout<<"size="<<iVector.size()<<" capacity="<<iVector.capacity()<<"  max_size="<<iVector.max_size()<<endl;
    //size=5 capacity=6  max_size=1073741823
   
    //reserve方法
    iVector.reserve(20);
    cout<<"After reserve: size="<<iVector.size()<<" capacity="<<iVector.capacity()<<"  max_size="<<iVector.max_size()<<endl;
    //After reserve: size=5 capacity=20  max_size=1073741823
   
    //resize方法
    iVector.resize(100);
    cout<<"After resize: size="<<iVector.size()<<" capacity="<<iVector.capacity()<<"  max_size="<<iVector.max_size()<<endl;
    //After resize: size=100 capacity=100  max_size=1073741823
   
    //begin方法
    iVector.resize(5);//修改为5
    INTVECTOR::iterator iter;
    iter = iVector.begin();
    cout<<"First element:"<<*iter<<endl;
    //First element:2
   
    //insert方法
    //PrintIntVector(iVector);
    iter = iVector.insert(iter, 65);
    iVector.insert(iter+1, 2, 30);
    iVector.insert(iVector.end(), iVector.begin(), iVector.end());//复制
    PrintIntVector(iVector);
    //Vector: [65,30,30,2,3,4,1,10,65,30,30,2,3,4,1,10]
   
    //at方法
    cout<<"Fifth element:"<<iVector.at(4)<<endl;
    //Fifth element:3
   
    //front 和 back方法
    cout<<"First element: "<<iVector.front()<< endl;
    cout<<"Last element: "<<iVector.back()<< endl;
    //First element: 65
    //Last element: 10
   
    //erase方法
    iVector.erase(iVector.begin());
    PrintIntVector(iVector);
    //Vector: [30,30,2,3,4,1,10,65,30,30,2,3,4,1,10]
   
    //reverse_iterator使用方法
    INTVECTOR::reverse_iterator riter;
    riter = iVector.rbegin();
    cout<<"Last element: "<<(*riter)<<endl;
    riter = iVector.rend();
    cout<<"First element: "<<*(riter-1)<<endl;
    //Last element: 10
    //First element: 30
   
    //随机访问方法
    cout<<"Eleventh element:"<<iVector[10]<<endl;
    //Eleventh element:2
   
    //clear方法
    iVector.clear();
    PrintIntVector(iVector);
    //Vector: []
   
    return 0;
}

所有输出:

Vector: [2,3,4,1,10,20]
Vector: [2,3,4,1,10]
size=5 capacity=6  max_size=1073741823
After reserve: size=5 capacity=20  max_size=1073741823
After resize: size=100 capacity=100  max_size=1073741823
First element:2
Vector: [65,30,30,2,3,4,1,10,65,30,30,2,3,4,1,10]
Fifth element:3
First element: 65
Last element: 10
Vector: [30,30,2,3,4,1,10,65,30,30,2,3,4,1,10]
Last element: 10
First element: 30
Eleventh element:2
Vector: []

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值