C++ day7 (STL标准库)

 vectors:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;
    if(v1.empty())
    {
        cout << "空" << endl;
    }
    
    //调用vectors中提供的size函数
    //cout << v1.capacity() << endl;
    v1.push_back(12);
    cout << v1.size() << endl;
    cout << "插入一个元素后capacity = " << v1.capacity() << endl;  //1
    v1.push_back(7);
    cout << "插入两个元素后capacity = " << v1.capacity() << endl;  //2
    v1.push_back(7);
    cout << "插入三个元素后capacity = " << v1.capacity() << endl;  //4  vector双倍扩容
    v1.push_back(10);
    v1.push_back(11);
    cout << "size = "<<v1.size() << endl;
    
    
    //打印元素的第一种写法
    int i;
    for(i=0;i<v1.size();i++)
    {
        //vector中对[]运算符重载
        cout<<"  【"<< i<<"】 =" << v1[i] << endl;
    }
    
    //打印元素的第二种写法,语法糖机制
    for(int j:v1)
    {
        cout << " 【"<< j<<"】 =" <<j << endl;
    }
    //begin返回的是第一个位置元素的迭代器---->理解为指针就可以了
    //cout << *v1.begin() << endl;
    //auto的作用自动获取,变量的类型,和C中的auto不是一个
    //打印元素的第三种方式
    for(auto it= v1.begin(); it != v1.end(); it++)
    {
        cout << "  【""】 =" <<*it << endl;
    }
    v1.pop_back();
    cout << v1.capacity() << endl;
    cout << v1.size() << endl;
    cout << v1.at(3) << endl;  //10
    
    vector<int> v3;
    v3 = v1;
    
    v3.assign(5,1);
    
    
    v3.at(2) = 89;   //at返回的是元素的引用
    //    v3.clear();
    v3.back() = 79;
    for(int j:v3)
    {
        cout << j << endl;
    }
    cout << v3.size() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值