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
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用法总结的更多相关文章
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector用法
基本操作 1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t&am ...
- C++ stl vector介绍
转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...
- C++-STL:vector用法总结
目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- 浅谈C++ STL vector 容器
浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- #include <vector>用法之我见
vector是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,(何为动态拓展,即是说如果你知道你要存的数据的个数,你定义的存储数据的数组大小也就决定了,但是若你事先不知道 ...
随机推荐
- #随笔之java匿名内部类
随笔之java匿名内部类 从今天起开始每日一篇技术博客,当然这只是我当天所学的一些随笔,里面或多或少会有理解不当的地方,希望大家多多指教,一起进步! 在讲匿名内部类之前,先讲讲内部类的一些概念. 1. ...
- kubernetes api文档
http://kubernetes.kansea.com/docs/api-reference/v1/definitions/
- 入门训练 Fibonacci数列 (水题)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n ...
- 【bzoj3998】弦论 后缀自动机
Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...
- centos7 docker 安装 mysql5.7.24 导入12G的sql
先在CentOS7里面安装docker Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker .通过 uname - ...
- UML之时序图详解
原文链接:https://blog.csdn.net/fly_zxy/article/details/80911942 什么是时序图 时序图(Sequence Diagram),又名序列图.循序图,是 ...
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...
- SDUT OJ 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- GCD BZOJ2818 [省队互测] 数学
题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入样例#1: 复制 4 ...
- mvc 请求处理管道
原文 http://blog.csdn.net/wulex/article/details/41514795 当一个asp.net mvc应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在 ...