vector的用法详解

1.vector

什么是vector呢? 让我简单的介绍一下它的特点.
1.它是一个容器,所谓容器也就是能够存储东西的器具,而在C++中它相当于一个动态的连续数组,能够存储各种类型的数据,它是一个顺序容器.
2.在底层中是一个类模板.能够实现许多非常强大的功能.

2.vector相关用法

我大概整理了它的常见用法,
#include <iostream>
#include <vector>
using namespace std;

int main(){

    //vector<int> v(10,1024);//容器中有10个1024
    vector<int> v = {1,2,3,5,6,8,10};//这样初始化容器容量为容器中元素的个数

    //v.assign(2,4);  //会改变元素的个数,但容量不变,元素变为2个,且值为4,如果之前没有设置容量大小,那么大小就为此时的2
    cout << "------------------" << endl;
    cout << boolalpha << v.empty() << endl;
    cout << v.at(0) << endl;
    //cout << v.at(2) << endl;//访问指定的元素,同时进行越界检查
    //cout << v[2] << endl; //operator[] 不会进行下标越界检查,不安全
    cout << v.front() << endl;  //访问第一个元素
    cout << v.back() << endl;   //访问最后一个元素
    int *p = v.data();  //返回数组中第一个元素的指针
    cout << p << endl;  
    cout << p[1] << endl;   //可以当作正常的指针进行访问
    //v.resize(100);  //如果capacity < newsize  
                      //例如,vector<int> v = {1,2,3,5,6,8,10};此时capcity=7,newsize=100,那么它会进行扩容至100,且将新扩容的元素置为0,那么此时元素个数为newsize=100
    //v.resize(4);      //如果capacity > newsize
                      //例如, vector<int> v = {1,2,3,5,6,8,10};此时capacity=7,newsize=4,那么他会删除多余元素,但容量保持不变.依然为7,size=newsize=4
    
    v.reserve(100);     //如果capacity < newsize
                        //例如, vector<int> v = {1,2,3,5,6,8,10};此时capacity=7,newsize=100,那么他会吧capacity置为newsize=100,但size保持不变,依然为7
    //v.reserve(4);       //如果capacity > newsize
                        //例如,  vector<int> v = {1,2,3,5,6,8,10};此时capacity=7,newsize=4,由于reserve()的作用是预留空间,此时新空间小于旧容量,他不会任何操作.
    cout << v.size() << endl;   //元素的个数
    cout << v.capacity() << endl;   //容器的容量
    cout << "-----------------------" << endl;
    v.shrink_to_fit();  //将未使用的内存空间释放,也就是将capacity 置到与 size 相等
    cout << v.size() << endl;
    cout << v.capacity() << endl;
    cout << "------------------" << endl;
    //v.clear();  //清除容器中的内容,也就是将size置为0,不改变capacity的大小
    //cout << v.capacity() << endl;
    cout << "--------------------" << endl;
    vector<int>::iterator it;//声明了一个vector的迭代器
    it = v.insert(v.begin() + 5, 50); //insert()的第一个参数为迭代器的变量,返回为当前迭代器的指向,将50放置到v[5]这个位置,其他元素后移,
                                      //如果此时size==capacity,那么它会先进行扩容,capacity = 2*capacity;
    cout << *it << endl;
    v.push_back(100);   //将元素放到容器末尾
    cout << v.size() << endl;
    cout << v.capacity() << endl;
    cout << "--------------------" << endl;
    it=v.emplace(v.begin()+2,10);  //用法跟insert()类似,但优于insert(),emplace()可以直接构造对象,不用产生临时变量,而insert()会产生一个临时变量,再将其放入到容器中    
    cout << *it << endl;
    v.emplace_back(200);    //直接在容器末尾构建元素
    cout << v.size() << endl;
    cout << v.capacity() << endl;   //移除容器末尾的元素
    cout << "--------------------" << endl;
    v.pop_back();
    cout << "--------------------" << endl;
    for (it = v.begin(); it != v.end();it++)
        cout << *it << " ";
    cout << endl;
    cout << "---------------------" << endl;
    vector<int>::reverse_iterator vt;   //定义一个逆向迭代器
    for (vt = v.rbegin(); vt != v.rend();vt++)
        cout << *vt << " ";
    cout << endl;
    cout << "-----------------" << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值