一:介绍

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
vector<int> v1();
for (int i=; i<; i++)
{
v1[i] = i;
}
//存储方式2
vector<int> v2;
for (int i=; i<; i++)
{
v2.push_back(i);
}

存储结构体和结构体指针

     struct Student
{
char name[];
int age;
}; //存储结构体
vector<Student> vStu1;
for (int i=; i<; i++)
{
Student stu;
strcpy(stu.name, "woniu201");
stu.age = + i;
vStu1.push_back(stu);
}
//存储结构体指针
vector<Student*> vStu2;
for (int i=; i<; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "woniu201");
pStu->age = + i;
vStu2.push_back(pStu);
}

四:遍历

     vector<int> v;
for (int i=; i<; i++)
{
v.push_back(i);
}
//遍历方式1
for (int i=; i<; i++)
{
int& a = v[i];
printf("%d ", a);
}
//遍历方式2
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
int&a = *it;
printf("%d ", a);
}

五:排序

对vector整形进行排序

 #include "stdlib.h"
#include <vector>
#include <algorithm> using namespace std; //升序比较函数
int compare1(const int &a, const int &b)
{
return a < b;
} //降序比较函数
int compare2(const int &a, const int &b)
{
return a > b;
} int main()
{
vector<int> v;
for (int i=; i<; i++)
{
v.push_back(rand() % );
} //遍历输出
printf("排序前数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} //升序排序
sort(v.begin(), v.end(), compare1); //遍历输出
printf("\n升序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} //降序排序
sort(v.begin(), v.end(), greater<int>()); //遍历输出
printf("\n降序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} getchar();
return ;
}

对存放类成员变量排序

 #include <string>
#include <vector>
#include <algorithm>
using namespace std; class Student {
public:
Student(string n, int c) :name(n), core(c) {} string name;
int core;
}; //升序比较函数
bool compare1(const Student& s1, const Student& s2)
{
return s1.core < s2.core;
} //降序比较函数
bool compare2(const Student& s1, const Student& s2)
{
return s1.core > s2.core;
} int main()
{
vector<Student> v;
Student s1("aaaa", );
Student s2("bbbb", );
Student s3("cccc", ); v.push_back(s1);
v.push_back(s2);
v.push_back(s3); printf("排序前数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
} //升序排序
sort(v.begin(), v.end(), compare1); printf("\n升序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
} //降序排序
sort(v.begin(), v.end(), compare2);
printf("\n降序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
getchar();
return ;
}

六:查找

     vector<int>::iterator it = find(v.begin(), v.end(), );
if(it != v.end())
{
cout << "found";
}
else
{
cout << "not found";
}

七:删除

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

八:释放内存

存放整形vector释放

     //存放整型
vector<int> v;
for (int i=; i<; i++)
{
v.push_back(i);
}
//释放内存
vector<int> (v).swap(v);

存放结构体vector释放

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

存放结构体指针vector释放

     //存储结构体指针
vector<Student*> vStu2;
for (int i=; i<; i++)
{
Student* pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "wangpengfei");
pStu->age = + i;
vStu2.push_back(pStu);
}
//释放内存
for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
{
if (NULL != *it)
{
delete *it;
*it = NULL;
}
}

扫码关注公众号

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

STL:vector用法总结的更多相关文章

  1. STL vector用法介绍

    STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...

  2. STL vector 用法介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  3. STL vector用法

    基本操作 1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t&am ...

  4. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

  5. C++-STL:vector用法总结

    目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...

  6. stl——vector详解

    stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...

  7. 浅谈C++ STL vector 容器

    浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...

  8. C++ STL vector容器学习

    STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...

  9. #include <vector>用法之我见

    vector是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,(何为动态拓展,即是说如果你知道你要存的数据的个数,你定义的存储数据的数组大小也就决定了,但是若你事先不知道 ...

随机推荐

  1. Delphi xe7 up1 调用android振动功能

    Delphi xe7 up1 调用android振动功能 振动用到以下4个单元: Androidapi.JNI.App,Androidapi.JNIBridge,Androidapi.JNI.Os,A ...

  2. 【C#】CLR

    CLR是如何工作的 借用维基百科上的一副图来描述CLR的运行流程: 从源代码到应用程序执行CLR主要做了以下工作: 将源代码编译成托管模块 托管模块是一个标准的 32 位 Microsoft Wind ...

  3. 【leetcode 94. 二叉树的中序遍历】解题报告

    前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...

  4. 汇编工具安装二:RadASM的安装!

    已经配置好的汇编工具下载地址:http://download.csdn.net/detail/sunylat/9189543 RadASM也是一款汇编开发工具,网址:http://www.oby.ro ...

  5. winform列标题高度无法改变

    datagridview行为里把ColumnHeadersHeightSizeMode属性设置为EnableResizing 但好像会导致横向滚动条从底部跑到了中间,还不知道这个BUG的原因,将Col ...

  6. requests模块处理cookie,代理ip,基于线程池数据爬取

    引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的. 一.基于requests模块 ...

  7. js自定义对象 (转)

    原文地址:https://sjolzy.cn/js-custom-object.html 29 March 2010 9:53 Monday by 小屋 javascript进阶之对象篇 一,概述 在 ...

  8. Qt 学习之路 2(42):QListWidget、QTreeWidget 和 QTableWidget

    Qt 学习之路 2(42):QListWidget.QTreeWidget 和 QTableWidget 豆子 2013年2月5日 Qt 学习之路 2 38条评论 上一章我们了解了 model/vie ...

  9. 三元运算符,i++(先用后加) ++i (先加后用)区别

    三元运算符是软件编程中的一个固定格式,语法是“条件表达式?表达式1:表达式2”.使用这个算法可以使调用数据时逐级筛选. 表达式:“()? :”. ()中进行二元运算 ?在运算,就形成三元运算符   i ...

  10. Vuex 使用了 module 后的访问方法 ..

    如果 使用了  module 和 namespace state 数据:=>   this.$store.state.User.info  (user 是模块名字. info 是 state 里 ...