一:介绍

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. LeftStr函数使用

    LeftStr(s, i); 表示返回字符串s的左边共I位字符的一个新字符串. var i: integer; s: string; result: string; begin i := ; s := ...

  2. 理解java中的值传递与“引用传递”

    额....java中其实没有引用传递 对于引用类型 ,在调用方法后,直接拷贝了引用的副本,但是它们指向了相同的堆地址,所以看起来像引用传递,但其实是值传递,只不过传递的引用的副本. 说一说为什么Str ...

  3. 学习React中遇到的问题

    1.执行eject后,再次启动项目报错 情景:使用create-react-app搭建了项目,启动没有问题,然后执行 $ yarn eject 暴露出webpack配置文件等,再次 $ yarn st ...

  4. 使用Jenkins远程部署war包到tomcat container

    Jenkins首先使用maven将源代码进行编译打包,之后需要将war包传送到tomcat服务器上进行部署. 来看一下Jenkins的基本配置,首先需要安装插件"Deploy to cont ...

  5. 转:[web]javascript 增加表單的input

    利用javascript增加form的input 這是js的部份 //用來區分不同input的name var element_count = 0; function add_element(obj) ...

  6. URLRewrite 实现方法详解

    所谓的伪静态页面,就是指的URL重写,在ASP.NET中实现非常简单首先你要在你的项目里引用两个DLL:ActionlessForm.dll.URLRewriter.dll,真正实现重写的是 URLR ...

  7. C# 调接口

    上一个项目,需要mvc管理后台调接口项目,以便后期的重构扩展,调研后发现后台用的ajax请求,直接调接口可能会有跨域问题,最终在c#代码中实现了这个需求. 1,Ajax请求后台 将接口所需参数传入 2 ...

  8. java 读取excel 2007 .xlsx文件 poi实现

    工作需要读取excel里面的行内容,使用java实现较为简单. 在最开始,尝试使用 jxl-2.6.12 来实现读取excel 的行内容.但是按照网上的方法,程序根本无法正确处理文件流.经过谷姐的一番 ...

  9. 自定义内核启动后的Logo

    1.使用图像GIMP工具   2.详细步骤如下:   A.将800x480的图片导入到GIMP工具.   B.选中GIMP菜单栏进行以下操作     图像         -->模式       ...

  10. get请求和post的请求的区别

    https://www.cnblogs.com/logsharing/p/8448446.html