6 STL-vector
重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!
生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦!
写在前面,本篇章主要介绍STL中常用容器vector。
1.1 vector的基本概念
vector数据结构和数组非常相似,也称为单端数组。
vector与普通数组的区别:
数组是静态空间,而vector可以自动动态扩展空间。
什么叫动态扩展?
- 并不是在原空间后继续增大新空间
- 而是找大更大的内存空间,然后将原数据拷贝到新空间,并释放原空间。
vector特点:
- 前端封闭,不能进行插入和删除,通常在尾部进行插入和删除。push_back()插入,pop_back()删除。
- v.begin()指向第一个元素位置,v.end()指向最后一个元素的下一个位置。
- v.rbegin()指向倒数第一个元素位置,v.rend()指向一个元素的前一个位置。
- 还提供很多其他接口,例如插入insert()、erase()删除等。
- 迭代器支持随机访问。可跳跃式访问,例如 it 指向迭代器,可以加n。

编辑
1.2 vector构造函数
函数原型:
- vector<T> v; //采用模板实现类实现,默认构造函数
- vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
- vector(n, elem); //构造函数将n个elem拷贝给本身。
- vector(const vector& vec); //拷贝构造函数。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//vector<T> v; //采用模板实现类实现,默认构造函数
//vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
//vector(n, elem); //构造函数将n个elem拷贝给本身。
//vector(const vector& vec); //拷贝构造函数。
void printVector(vector<int>& v) {
for (int item : v) {
cout << item << " ";
}
cout << endl;
}
void test() {
vector<int> v1;
for (int i = 1; i < 6; i++) {
v1.push_back(i);
}
printVector(v1);
vector<int> v2(v1.begin(), v1.end());
// 用区间的方式来构造vector,只要传入的是一个区间就可以拷贝区间内的数据到新的vector
//vector<int> v2(v1.begin(), v1.begin() + 3);
printVector(v2);
vector<int> v3(10,5);
printVector(v3);
vector<int> v4(v3);
printVector(v4);
}
int main() {
test();
system("pause");
return 0;
}

1.3 vector赋值操作
函数原型:
- vector& operator=(const vector& vec); //重载等号操作符
- assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
- assign(n, elem); //将n个elem拷贝赋值给本身。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//vector& operator=(const vector& vec); //重载等号操作符
//assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
//assign(n, elem); //将n个elem拷贝赋值给本身。
void printVector(vector<int>& v) {
for (int item : v) {
cout << item << " ";
}
cout << endl;
}
void test() {
vector<int> v1;
for (int i = 1; i < 6; i++) {
v1.push_back(i);
}
printVector(v1);
//vector& operator=(const vector& vec); //重载等号操作符
vector<int> v2;
v2 = v1;
printVector(v2);
//assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
vector<int> v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//assign(n, elem);//将n个elem拷贝赋值给本身。
v3.assign(10, 3);
printVector(v3);
}
int main() {
test();
system("pause");
return 0;
}

1.4 vector容量大小
函数原型:
- empty(); //判断容器是否为空
- capacity(); //容器的容量
- size(); //返回容器中元素的个数
- resize(int num); //重新指定容器长度为num,若容器变长,则以默认值填充新位置。 //如果容器变短,则末尾超出容器长度的元素被删除。
- resize(int num, elem); //重新指定容器长度为num,若容器变长,则以elem值填充新位置。 //如果容器变短,则末尾超出容器长度的元素被删除
注意:
- 若重新指定容器长度,若容器变长,则capacity()容器的容量和size()都会变长。
- 若后续又指定容器变短,则capacity()容器的容量不变,只有size()会变小。
- 只有size()可变大变小,capacity()容器的容量只会变大。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//empty(); //判断容器是否为空
//capacity(); //容器的容量
//size(); //返回容器中元素的个数
//resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
// //如果容器变短,则末尾超出容器长度的元素被删除。
//resize(int num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
// //如果容器变短,则末尾超出容器长度的元素被删除
void printVector(vector<int> v) {
for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
vector<int> v1;
for (int i = 1; i < 6; i++) {
v1.push_back(i);
}
printVector(v1);
if (v1.empty()) {
cout << "v1 为空" << endl;
}
else {
cout << "v1 不为空" << endl;
cout <<"capacity:"<< v1.capacity() << endl;
cout << "size:" << v1.size() << endl;
}
// 手动重新指定容器长度变大为10
v1.resize(10);
cout << "v1 手动重新指定容器长度变大" << endl;
cout << "capacity:" << v1.capacity() << endl; // 容量会扩展成10
cout << "size:" << v1.size() << endl; // 元素个数会变为10
printVector(v1); // 以默认值0填充
// 手动重新指定容器长度变小为3
v1.resize(3);
cout << "v1 手动重新指定容器长度变小" << endl;
cout << "capacity:" << v1.capacity() << endl; // ***容量不变 10
cout << "size:" << v1.size() << endl; // 元素个数会变少 3
printVector(v1); // 如果容器变短,则末尾超出容器长度的元素被删除。
// resize(int num, elem);利用重载版本,指定默认值填充方式,
v1.resize(5,10);
cout << "v1 指定默认值填充方式" << endl;
cout << "capacity:" << v1.capacity() << endl; // ***容量不变 10
cout << "size:" << v1.size() << endl; // 元素个数等于指定个数5
printVector(v1); //如果重新指定的比原来的长,用默认值填充10
}
int main() {
test();
system("pause");
return 0;
}

1.5 vector插入和删除
函数原型:
- push_back(ele); //尾部插入元素ele
- pop_back(); //删除最后一个元素
- insert(const_iterator pos, ele); //在迭代器指向位置pos插入元素ele
- insert(const_iterator pos, int count, ele); //在迭代器指向位置pos插入count个元素ele
- erase(const_iterator pos); //删除迭代器指向pos位置的元素
- erase(const_iterator start, const_iterator end); //删除迭代器[start,end)之间的元素,左闭右开
- clear(); //删除容器中所有元素
erase(v.begin(), v.end()); 与clear()效果一样,都是清空数据。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//push_back(ele); //尾部插入元素ele
//pop_back(); //删除最后一个元素
//insert(const_iterator pos, ele); //在迭代器指向位置pos插入元素ele
//insert(const_iterator pos, int count, ele); //在迭代器指向位置pos插入count个元素ele
//erase(const_iterator pos); //删除迭代器指向pos位置的元素
//erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
//clear(); //删除容器中所有元素
void printVector(vector<int> v) {
for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
vector<int> v1;
for (int i = 1; i < 6; i++) {
v1.push_back(i);
}
printVector(v1);
// 尾删
v1.pop_back();
printVector(v1);
//在迭代器指向位置pos插入元素ele
v1.insert(v1.begin() + 1, 10);
printVector(v1);
//insert(const_iterator pos, int count, ele);
//在迭代器指向位置pos插入count个元素ele
v1.insert(v1.begin() + 1,3,10);
printVector(v1);
//erase(const_iterator pos);//删除迭代器指向pos位置的元素
v1.erase(v1.begin() + 1);
printVector(v1);
//erase(const_iterator start, const_iterator end);
//删除迭代器从[start,end)之间的元素,左闭右开
v1.erase(v1.begin(),v1.begin()+1);
printVector(v1);
//clear();//删除容器中所有元素
//v1.erase(v1.begin(), v1.end()); 也是清空数据
v1.clear();
printVector(v1);
}
int main() {
test();
system("pause");
return 0;
}

1.6 vector数据存取
函数原型:
- at(int idx); //返回索引idx所指的数据
- operator[]; //返回索引idx所指的数据
- front(); //返回容器中第一个数据元素
- back(); //返回容器中最后一个数据元素
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//at(int idx); //返回索引idx所指的数据
//operator[]; //返回索引idx所指的数据
//front(); //返回容器中第一个数据元素
//back(); //返回容器中最后一个数据元素
void test() {
vector<int> v1;
for (int i = 1; i < 6; i++) {
v1.push_back(i);
}
cout << v1.at(3) << endl; // 第三个
cout << v1[3]<< endl; // 第三个
cout << v1.front() << endl; // 第一个
cout << v1.back() << endl; // 最后一个
}
int main() {
test();
system("pause");
return 0;
}

1.7 vector互换容器
函数原型:
swap(v); 将两个vector容器互换
实际用途:
巧用swap可以收缩内存空间。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
void printVector(vector<int> v) {
for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
cout << "交换前:" << endl;
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
printVector(v1);
vector<int> v2;
for (int i = 9; i >=0; i--) {
v2.push_back(i);
}
printVector(v2);
cout << "交换后:" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
// 实际用途
// 巧用swap可以收缩内存空间
void test2() {
vector<int> v;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl; // 138255
cout << "v的长度为:" << v.size() << endl; // 100000
v.resize(3); // 重新指定大小。当大小变的很小时,capacity还是占用空间很大,不会缩小
cout << "v的容量为:" << v.capacity() << endl; // 138255
cout << "v的长度为:" << v.size() << endl; // 3
// 巧用swap收缩内存
// vector<int>(v) 利用v示例化一个匿名对象容器。这个容器容量和长度只有3
// swap(v) 交换匿名对象容器和v
vector<int>(v).swap(v);
cout << "v的容量为:" << v.capacity() << endl; // 3
cout << "v的长度为:" << v.size() << endl; // 3
}
int main() {
test2();
system("pause");
return 0;
}

1.8 vector预留空间
作用:
减少vector动态扩展容量的次数,提升性能。
函数原型:
reserve(int len); // 容器预留len个元素长的容量,预留位置的元素不会被初始化,元素不可访问。
使用时机:
如果知道数据量很大,就可以先使用reserve来预留空间。
示例:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
void test() {
vector<int> v;
// 利用预留空间
v.reserve(100000);
// cout << v[0] << endl; // 会报错,0位置元素不可访问,未初始化。
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
if (p != &v[0]) { // 如果v做了动态内存扩展p就不会指向&v[0]
p = &v[0]; // 让指针p指向v的首地址
num++; // 如果v做了动态内存扩展,num就增加一次
}
}
cout << num << endl;
}
int main() {
test();
system("pause");
return 0;
}

6 STL-vector的更多相关文章
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector+sort排序和multiset/multimap排序比较
由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector使用方法介绍
介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- C++STL vector详解(杂谈)
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- C++ stl vector介绍
转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...
- 浅谈C++ STL vector 容器
浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...
随机推荐
- java的数据类型分为两大类
java的数据类型分为两大类 基本类型(primitive type) 数据类型 整数类型 byte占一个字节范围:-128-127 short占两个字节范围:-32768-32767 int占四个字 ...
- 【Java面试】面试遇到宽泛的问题,这么回答就稳了,谈谈你对Redis的理解
"谈谈你对Redis的理解"! 面试的时候遇到这类比较宽泛的问题,是不是很抓狂? 是不是不知道从何开始说起? 没关系,今天我用3分钟教你怎么回答. 大家好,我是Mic,一个工作了1 ...
- Rust-函数
函数结构 函数要点 函数名和变量名使用蛇形命名法(snake case),例如 fn add_two() -> {} 函数的位置可以随便放,Rust 不关心我们在哪里定义了函数,只要有定义即可 ...
- 第六章:Django 综合篇 - 14:Django 日志
Django使用Python内置的logging模块实现它自己的日志系统. 如果你没有使用过logging模块,请参考Python教程中的相关章节. 直达链接<logging模块详解>. ...
- Kibana可视化数据(Visualize)
在侧边导航栏点击 Visualize 开始视化您的数据. Visualize 工具能让您通过多种方式浏览您的数据.例如:我们使用饼图这个重要的可视化控件来查看银行账户样本数据中的账户余额.点击屏幕中间 ...
- Compose 模板文件
模板文件是使用 Compose 的核心,涉及到的指令关键字也比较多.但大家不用担心,这里面大部分指令跟 docker run 相关参数的含义都是类似的. 默认的模板文件名称为 docker-compo ...
- Prometheus告警处理
在Prometheus Server中定义告警规则以及产生告警,Alertmanager组件则用于处理这些由Prometheus产生的告警.Alertmanager即Prometheus体系中告警的统 ...
- 论文解读(FedGAT)《Federated Graph Attention Network for Rumor Detection》
论文信息 论文标题:Federated Graph Attention Network for Rumor Detection论文作者:Huidong Wang, Chuanzheng Bai, Ji ...
- 掌控(control) 方法记录
掌控(control) 题面描述 公元\(2044\)年,人类进入了宇宙纪元.L国有\(n\)个星球,分别编号为\(1\)到\(n\),每一星球上有一个球长.有些球长十分强大,可以管理或掌控其他星球的 ...
- 糟了,线上服务出现OOM了
前言 前一段时间,公司同事的一个线上服务OOM的问题,我觉得挺有意思的,在这里跟大家一起分享一下. 我当时其实也参与了一部分问题的定位. 1 案发现场 他们有个mq消费者服务,在某一天下午,出现OOM ...