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是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,(何为动态拓展,即是说如果你知道你要存的数据的个数,你定义的存储数据的数组大小也就决定了,但是若你事先不知道 ...
随机推荐
- .Net中的并行编程-1.路线图(转)
大神,大神,膜拜膜拜,原文地址:http://www.cnblogs.com/zw369/p/3834559.html 目录 .Net中的并行编程-1.路线图 分析.Net里线程同步机制 .Net中的 ...
- 解决低版本Xcode不支持高版本iOS真机调试的问题
1.现象截图 Could not locate device support files. This iPhone 6s is running iOS 11.1 (15B93), which may ...
- jQuery定位导航滚动3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php中的openssl开启方法
windows下开启方法: 1: 首先检查php.ini中:extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘:', 如果不存在这行,那么添加extensio ...
- caffe/proto/caffe.pb.h: No such file or director
caffe编译过程中遇到的为问题: fatal error: caffe/proto/caffe.pb.h: No such file or directory 解决方法: 用protoc从caffe ...
- quill 设置 初始值...
1down voteaccepted For your first issue change this: text.value = JSON.stringify(quill.root.innerHTM ...
- 编写高质量代码:Web前端开发修炼之道(一)
最近老大给我们买来一些技术方面的书籍,其实很少搬着一本书好好的完整的看完过,每每看电子档的,也是打游击式的看看这章,瞅瞅那章,在那5本书中挑了一本比较单薄的<编写高质量代码web前端开发修炼之道 ...
- ubuntu下安装和更新R语言
R官网更新说明 https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu/README.html 本文主要讲解在ubuntu下如何安装和更新 ...
- C++_新特性总结与未来的路
了解C++之后,可以阅读一些高级主题和面向对象编程相关的书籍: OOP有助于开发大型的项目,并提高其可靠性: OOP方法的基本活动之一就是发明能够模拟当前情况的类.当前情况被统称为问题域. 由于实际问 ...
- 多气体组分DEM流动的DMP并行内存错误
今天踩到一个坑.调DEM反应的时候,气体需要设置为多组分,这时就不能用 DES_INTERP_ON = .T. DES_INTERP_SCHEME = 'GARG_2012' 这个差值格式了,否则DM ...