zoukankan      html  css  js  c++  java
  • STL学习系列二:Vector容器

    1.Vector容器简介

    • vector是将元素置于一个动态数组中加以管理的容器。
    • vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲)。
    • vector尾部添加或移除元素非常快速。但是在中部或头部插入元素或移除元素比较费时。

    2.vector对象的默认构造

    vector采用模板类实现,vector对象的默认构造形式

    vector<T> vecT; 

    vector<int> vecInt;          //一个存放int的vector容器。

    vector<float> vecFloat;     //一个存放float的vector容器。

    3.vector对象的带参数构造

    • vector(beg,end);    //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。
    • vector(n,elem);   //构造函数将n个elem拷贝给本身。
    • vector(const vector &vec);  //拷贝构造函数
     1 #include <iostream>
     2 using namespace std;
     3 #include <vector>
     4 
     5 void objPlay()
     6 {
     7     int  iArray[] = { 0, 1, 2, 3, 4 };
     8     vector<int> vecIntA(iArray, iArray + 5);
     9     vector<int> vecIntB(vecIntA.begin(), vecIntA.end());   //用构造函数初始化容器vecIntB 
    10     vector<int> vecIntB(vecIntA.begin(), vecIntA.begin() + 3);
    11     vector<int> vecIntC(3, 9); //此代码运行后,容器vecIntB就存放3个元素,每个元素的值是9。
    12     vector<int> vecIntD(vecIntA);
    13 }
    14 int main()
    15 {
    16     objPlay();
    17     system("pause");
    18 }

    4.vector的赋值

    • vector.assign(beg,end);    //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
    • vector.assign(n,elem);  //将n个elem拷贝赋值给本身。
    • vector& operator=(const vector  &vec);          //重载等号操作符
    • vector.swap(vec);  // 将vec与本身的元素互换。
    void objPlay2()
    {
        vector<int> vecIntA, vecIntB, vecIntC, vecIntD;
        int  iArray[] = { 0, 1, 2, 3, 4 };
        vecIntA.assign(iArray, iArray + 5);
        vecIntB.assign(vecIntA.begin(), vecIntA.end());    //用其它容器的迭代器作参数。
        vecIntC.assign(3, 9);
        vecIntD = vecIntA;
        vecIntA.swap(vecIntD);
    }

    5.vector的大小

    • vector.size();         //返回容器中元素的个数
    • vector.empty();     //判断容器是否为空
    • vector.resize(num);   //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
    • vector.resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
    void objPlay3()
    {
        vector<int> vecInt;
        int  iArray[] = {  1, 2, 3 };
        int iSize = vecInt.size();        //iSize == 3;
        bool bEmpty = vecInt.empty();    // bEmpty == false;
        vecInt.resize(5);  //此时里面包含1,2,3,0,0元素。
        vecInt.resize(8, 3);  //此时里面包含1,2,3,0,0,3,3,3元素。
        vecInt.resize(2);  //此时里面包含1,2元素。
    }

     6.vector末尾的添加移除操作

    • vector.push_back();//向容器的尾部插入元素
    • vector.pop_back();//从容器的尾部弹出元素
    void objPlay4()
    {
        vector<int> vecInt;
        vecInt.push_back(1);  //在容器尾部加入一个元素
        vecInt.push_back(3);  
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器有5个元素,1,3,5,7,9
        vecInt.pop_back();  //弹出尾部的一个元素
        vecInt.pop_back();  //此时容器只有3个元素,1,3,5
    }

    7.vector的数据随机存取

    • vec.at(idx);    //返回索引idx所指的数据,如果idx越界,抛出out_of_range异常。
    • vec[idx];          //返回索引idx所指的数据,越界时,运行直接报错
    void objPlay5()
    {
        int  iArray[] = { 1, 3, 5,7,9 };
        vector<int> vecInt(iArray,iArray+5); //包含1 ,3 ,5 ,7 ,9
        vecInt.at(2) == vecInt[2];            //5
        vecInt.at(2) = 8;                  // 或  vecInt[2] = 8;
    
        int iF = vecInt.front();         //iF==1
        int iB = vecInt.back();            //iB==9
        vecInt.front() = 11;           //vecInt包含{11,3,8,7,9}
        vecInt.back() = 19;              //vecInt包含{11,3,8,7,19}
    }

    8.vector的插入

    • vector.insert(pos,elem);   //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
    • vector.insert(pos,n,elem);   //在pos位置插入n个elem数据,无返回值。
    • vector.insert(pos,beg,end);   //在pos位置插入[beg,end)区间的数据,无返回值
    void objPlay6()
    {
        vector<int> vecA;
        vector<int> vecB;
    
        vecA.push_back(1);
        vecA.push_back(3);
        vecA.push_back(5);
        vecA.push_back(7);
        vecA.push_back(9);
    
        vecB.push_back(2);
        vecB.push_back(4);
        vecB.push_back(6);
        vecB.push_back(8);
    
        vecA.insert(vecA.begin(), 11);        //{11, 1, 3, 5, 7, 9}
        vecA.insert(vecA.begin() + 1, 2, 33);        //{11,33,33,1,3,5,7,9}
        vecA.insert(vecA.begin(), vecB.begin(), vecB.end());    //{2,4,6,8,11,33,33,1,3,5,7,9}
    }

    9.vector的删除

    • vector.clear();      //移除容器的所有数据
    • vec.erase(beg,end);  //删除[beg,end)区间的数据,返回下一个数据的位置。
    • vec.erase(pos);    //删除pos位置的数据,返回下一个数据的位置。
    void objPlay7()
    {
        vector<int> vecInt;
        vecInt.push_back(1);  
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器元素是1,3,5,7,9
        vector<int>::iterator itBegin = vecInt.begin() + 1;
        vector<int>::iterator itEnd = vecInt.begin() + 2;
        vecInt.erase(itBegin, itEnd); //删除后 1,5,7,9(左闭右开)
        for (vector<int>::iterator it = vecInt.begin(); it != vecInt.end();)    //小括号里不需写  ++it
        {
            if (*it == 3)
            {
                it = vecInt.erase(it);      //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器  ,此处不能it++,因为删除后迭代器自动后移
            }
            else
            {
                ++it;
            }
        }
        //删除vecInt的所有元素
        vecInt.clear();            //容器为空
    }

    10.迭代器基本原理

    • 迭代器是一个可遍历STL容器内全部或部分元素的对象。
    • 迭代器指出容器中的一个特定位置。
    • 迭代器就如同一个指针。
    • 迭代器提供对一个容器中的对象的访问方法,并且可以定义了容器中对象的范围。

    迭代器的分类

      输入迭代器:也有叫法称之为“只读迭代器”,它从容器中读取元素,只能一次读入一个元素向前移动,只支持一遍算法,同一个输入迭代器不能两遍遍历一个序列。

      输出迭代器:也有叫法称之为“只写迭代器”,它往容器中写入元素,只能一次写入一个元素向前移动,只支持一遍算法,同一个输出迭代器不能两遍遍历一个序列。

      正向迭代器:组合输入迭代器和输出迭代器的功能,还可以多次解析一个迭代器指定的位置,可以对一个值进行多次读/写。

      双向迭代器:组合正向迭代器的功能,还可以通过--操作符向后移动位置。

    11.双向迭代器与随机访问迭代器

    双向迭代器支持的操作:it++,  ++it,    it--,   --it,*it, itA = itB

    随机访问迭代器支持的操作:it+=i, it-=i, it+i(或it=it+i),it[i]

    void objPlay8()
    {
        vector<int> vecInt;
        vecInt.push_back(1);
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器元素是1,3,5,7,9
        for (vector<int>::iterator it = vecInt.begin(); it != vecInt.end(); ++it)
        {
            int iItem = *it;
            cout << iItem;    //或直接使用  cout << *it;
        }
        //打印出1 3 5 7 9
        for (vector<int>::reverse_iterator rit = vecInt.rbegin(); rit != vecInt.rend(); ++rit)    //注意,小括号内仍是++rit
        {
            int iItem = *rit;
            cout << iItem;    //或直接使用cout << *rit;
        }
      //打印出9, 7, 5, 3, 1
    }

    以上的完整版程序如下:

    #include <iostream>
    using namespace std;
    #include <vector>
    
    void objPlay()
    {
        int  iArray[] = { 0, 1, 2, 3, 4 };
        vector<int> vecIntA(iArray, iArray + 5);
        vector<int> vecIntB(vecIntA.begin(), vecIntA.end());   //用构造函数初始化容器vecIntB 
        vector<int> vecIntC(vecIntA.begin(), vecIntA.begin() + 3);
        vector<int> vecIntD(3, 9); //此代码运行后,容器vecIntB就存放3个元素,每个元素的值是9。
        vector<int> vecIntE(vecIntA);
    }
    void objPlay2()
    {
        vector<int> vecIntA, vecIntB, vecIntC, vecIntD;
        int  iArray[] = { 0, 1, 2, 3, 4 };
        vecIntA.assign(iArray, iArray + 5);
        vecIntB.assign(vecIntA.begin(), vecIntA.end());    //用其它容器的迭代器作参数。
        vecIntC.assign(3, 9);
        vecIntD = vecIntA;
        vecIntA.swap(vecIntD);
    }
    void objPlay3()
    {
        vector<int> vecInt;
        int  iArray[] = {  1, 2, 3 };
        int iSize = vecInt.size();        //iSize == 3;
        bool bEmpty = vecInt.empty();    // bEmpty == false;
        vecInt.resize(5);  //此时里面包含1,2,3,0,0元素。
        vecInt.resize(8, 3);  //此时里面包含1,2,3,0,0,3,3,3元素。
        vecInt.resize(2);  //此时里面包含1,2元素。
    }
    void objPlay4()
    {
        vector<int> vecInt;
        vecInt.push_back(1);  //在容器尾部加入一个元素
        vecInt.push_back(3);  
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器有5个元素,1,3,5,7,9
        vecInt.pop_back();  //弹出尾部的一个元素
        vecInt.pop_back();  //此时容器只有3个元素,1,3,5
    }
    void objPlay5()
    {
        int  iArray[] = { 1, 3, 5,7,9 };
        vector<int> vecInt(iArray,iArray+5); //包含1 ,3 ,5 ,7 ,9
        vecInt.at(2) == vecInt[2];            //5
        vecInt.at(2) = 8;                  // 或  vecInt[2] = 8;
    
        int iF = vecInt.front();         //iF==1
        int iB = vecInt.back();            //iB==9
        vecInt.front() = 11;           //vecInt包含{11,3,8,7,9}
        vecInt.back() = 19;              //vecInt包含{11,3,8,7,19}
    }
    void objPlay6()
    {
        vector<int> vecA;
        vector<int> vecB;
    
        vecA.push_back(1);
        vecA.push_back(3);
        vecA.push_back(5);
        vecA.push_back(7);
        vecA.push_back(9);
    
        vecB.push_back(2);
        vecB.push_back(4);
        vecB.push_back(6);
        vecB.push_back(8);
    
        vecA.insert(vecA.begin(), 11);        //{11, 1, 3, 5, 7, 9}
        vecA.insert(vecA.begin() + 1, 2, 33);        //{11,33,33,1,3,5,7,9}
        vecA.insert(vecA.begin(), vecB.begin(), vecB.end());    //{2,4,6,8,11,33,33,1,3,5,7,9}
    }
    void objPlay7()
    {
        vector<int> vecInt;
        vecInt.push_back(1);  
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器元素是1,3,5,7,9
        vector<int>::iterator itBegin = vecInt.begin() + 1;
        vector<int>::iterator itEnd = vecInt.begin() + 2;
        vecInt.erase(itBegin, itEnd); //删除了1,5,7,9
        for (vector<int>::iterator it = vecInt.begin(); it != vecInt.end();)    //小括号里不需写  ++it
        {
            if (*it == 3)
            {
                it = vecInt.erase(it);      //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器  ,此处不能it++,因为删除后迭代器自动后移
            }
            else
            {
                ++it;
            }
        }
        //删除vecInt的所有元素
        vecInt.clear();            //容器为空
    }
    void objPlay8()
    {
        vector<int> vecInt;
        vecInt.push_back(1);
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);//此时容器元素是1,3,5,7,9
        for (vector<int>::iterator it = vecInt.begin(); it != vecInt.end(); ++it)
        {
            int iItem = *it;
            cout << iItem;    //或直接使用  cout << *it;
        }
        //打印出1 3 5 7 9
        for (vector<int>::reverse_iterator rit = vecInt.rbegin(); rit != vecInt.rend(); ++rit)    //注意,小括号内仍是++rit
        {
            int iItem = *rit;
            cout << iItem;    //或直接使用cout << *rit;
        }
      //打印出9, 7, 5, 3, 1
    }
    int main()
    {
        objPlay();
        objPlay2();
        objPlay3();
        objPlay4();
        objPlay5();
        objPlay6();
        objPlay7();
        objPlay8();
        system("pause");
    }
  • 相关阅读:
    docker基础命令
    Dubbo添加Filter过滤器
    Jvm FullGC 如何排查?
    ElastaticSearch----es聚合,并获取query的排序结果
    java8 StringJoiner拼接字符串
    ElastaticSearch--- es多字段聚合
    java使用Mysql批量更新(先查询,再插入或更新)
    Mybatis批量插入,批量更新
    谷粒商城学习——P51商品服务-API-三级分类-删除-删除效果细化
    谷粒商城学习——P50商品服务-API-三级分类-删除-逻辑删除
  • 原文地址:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_stl_002.html
Copyright © 2011-2022 走看看