zoukankan      html  css  js  c++  java
  • STL:vector用法总结

    一:介绍

    vector是C++标准模板库,是一个容器,底层是数组,为连续内存。
    命名空间为std,所属头文件为<vector>   注意:不是<vector.h>
    vector存储数据时,会分配一个存储空间,如果继续存储,该分配的空间已满,就会分配一块更大的内存,把原来的数据复制过来,继续存储,这些性能也会一定程度上会有损耗

    二:常用操作

    1.容量

    a.vector大小:vector.size()
    b.vector所占内存实际大小:vector.capacity()

    2.修改

    a.尾部添加元素:vector.push_back()
    b.尾部删除元素:vector.pop_back()
    c.交换两个vector元素:vector.swap()
    d.清空vector元素:vector.clear()
    e.删除指定元素:vector.erase(it)

    3.迭代器

    a.vector开始指针:vector.begin()
    b.vector尾部指针:vector.end()   注:最后一个元素的下一个位置,类似为NULL,不是容器的最后一个元素

    4.访问元素

    a.下标访问:vector[1]  //不检查是否越界
    b.at方法访问:vector.at(1) //自动检查是否越界,如越界会抛出异常
    c.访问第一个元素:vector.front()
    d.访问最后一个元素:vector.back()

    三:存储

    简单存储
     
     1     //存储方式1
     2     vector<int> v1(10);
     3     for (int i=0; i<10; i++)
     4     {
     5         v1[i] = i;
     6     }
     7     //存储方式2
     8     vector<int> v2;
     9     for (int i=0; i<10; i++)
    10     {
    11         v2.push_back(i);
    12     }

    存储结构体和结构体指针

     1     struct Student
     2     {
     3         char name[32];
     4         int  age;
     5     };
     6  
     7     //存储结构体
     8     vector<Student> vStu1;
     9     for (int i=0; i<10; i++)
    10     {
    11         Student stu;
    12         strcpy(stu.name, "woniu201");
    13         stu.age = 30 + i;
    14         vStu1.push_back(stu);
    15     }
    16     //存储结构体指针
    17     vector<Student*> vStu2;
    18     for (int i=0; i<10; i++)
    19     {
    20         Student* pStu = (Student*)malloc(sizeof(Student));
    21         strcpy(pStu->name, "woniu201"); 
    22         pStu->age = 30 + i; 
    23         vStu2.push_back(pStu); 
    24     }    

    四:遍历

     1     vector<int> v;
     2     for (int i=0; i<100; i++)
     3     {
     4         v.push_back(i);
     5     }
     6     //遍历方式1
     7     for (int i=0; i<100; i++)
     8     {
     9         int& a = v[i];
    10         printf("%d ", a);
    11     }
    12     //遍历方式2
    13     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    14     {
    15         int&a = *it;
    16         printf("%d ", a);
    17     }

    五:排序

    对vector整形进行排序

     1 #include "stdlib.h"
     2 #include <vector>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 //升序比较函数
     8 int compare1(const int &a, const int &b)
     9 {
    10     return a < b;
    11 }
    12 
    13 //降序比较函数
    14 int compare2(const int &a, const int &b)
    15 {
    16     return a > b;
    17 }
    18 
    19 int main()
    20 {
    21     vector<int> v;
    22     for (int i=0; i<10; i++)
    23     {
    24         v.push_back(rand() % 10);
    25     }
    26 
    27     //遍历输出
    28     printf("排序前数据:");
    29     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    30     {
    31         printf("%d ", *it);
    32     }
    33 
    34     //升序排序
    35     sort(v.begin(), v.end(), compare1);
    36 
    37     //遍历输出
    38     printf("
    升序后数据:");
    39     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    40     {
    41         printf("%d ", *it);
    42     }
    43 
    44     //降序排序
    45     sort(v.begin(), v.end(), greater<int>());
    46 
    47     //遍历输出
    48     printf("
    降序后数据:");
    49     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    50     {
    51         printf("%d ", *it);
    52     }
    53 
    54     getchar();
    55     return 1;
    56 }

    对存放类成员变量排序

     1 #include <string>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5  
     6 class Student {
     7 public:    
     8     Student(string n, int c) :name(n), core(c) {}
     9  
    10     string    name;
    11     int        core;
    12 };
    13  
    14 //升序比较函数
    15 bool compare1(const Student& s1, const Student& s2)
    16 {
    17     return s1.core < s2.core;
    18 }
    19  
    20 //降序比较函数
    21 bool compare2(const Student& s1, const Student& s2)
    22 {
    23     return s1.core > s2.core;
    24 }
    25  
    26  
    27 int main()
    28 {
    29     vector<Student> v;
    30     Student s1("aaaa", 97);
    31     Student s2("bbbb", 99);
    32     Student s3("cccc", 95);
    33  
    34     v.push_back(s1);
    35     v.push_back(s2);
    36     v.push_back(s3);
    37  
    38     printf("排序前数据:
    ");
    39     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    40     {
    41         printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    42     }
    43  
    44     //升序排序
    45     sort(v.begin(), v.end(), compare1);
    46  
    47     printf("
    升序后的数据:
    ");
    48     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    49     {
    50         printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    51     }
    52  
    53     //降序排序
    54     sort(v.begin(), v.end(), compare2);
    55     printf("
    降序后的数据:
    ");
    56     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    57     {
    58         printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    59     }
    60     getchar();
    61     return 1;
    62 }

    六:查找

    1     vector<int>::iterator it = find(v.begin(), v.end(), 5);
    2     if(it != v.end())
    3     {
    4         cout << "found";
    5     }
    6     else
    7     {
    8         cout << "not found";
    9     }

    七:删除

    1     for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
    2     {
    3         if(*it == 8)
    4         {
    5             it = v.erase(it);//it会++一次
    6             it--;       //删除完后需要--,否则最终循环越界
    7         }
    8     }

    八:释放内存

    存放整形vector释放

    1     //存放整型
    2     vector<int> v;
    3     for (int i=0; i<100; i++)
    4     {
    5         v.push_back(i);
    6     }
    7     //释放内存
    8     vector<int> (v).swap(v);

    存放结构体vector释放

        //存储结构体
        vector<Student> vStu1;
        for (int i=0; i<10; i++)
        {
            Student stu;
            strcpy(stu.name, "wangpengfei");
            stu.age = 30 + i;
            vStu1.push_back(stu);
        }
        //释放内存
        vector<Student> (vStu1).swap(vStu1);

    存放结构体指针vector释放

     1     //存储结构体指针
     2     vector<Student*> vStu2;
     3     for (int i=0; i<10; i++)
     4     {
     5         Student* pStu = (Student*)malloc(sizeof(Student));
     6         strcpy(pStu->name, "wangpengfei");
     7         pStu->age = 30 + i;
     8         vStu2.push_back(pStu);
     9     }
    10     //释放内存
    11     for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
    12     {
    13         if (NULL != *it)
    14         {
    15             delete *it;
    16             *it = NULL;
    17         }
    18     }

    扫码关注公众号

    专注分享Java,C/C++,STL,Spring框架,mybatis框架,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,在这里一起探讨,一起学习,一起进步,不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣。

  • 相关阅读:
    lsyncd实时同步搭建指南——取代rsync+inotify
    Linux内存管理
    [Python爬虫] 之十一:Selenium +phantomjs抓取活动行中会议活动信息
    [Python爬虫] 之十:Selenium +phantomjs抓取活动行中会议活动
    [Python爬虫] 之九:Selenium +phantomjs抓取活动行中会议活动(单线程抓取)
    python 正则表达式
    Scrapy 安装
    python 安装whl文件
    [Python爬虫] 之八:Selenium +phantomjs抓取微博数据
    [Python爬虫] 之七:selenium webdriver定位不到元素的五种原因及解决办法(转载)
  • 原文地址:https://www.cnblogs.com/woniu201/p/9833430.html
Copyright © 2011-2022 走看看