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\)的一种比较基本的容器 ...
随机推荐
- MySQL 中的锁机制
介绍锁机制 技术是为了解决问题而生的,锁被用来实现隔离性,保证并发事务的正确性. 两段锁 & 一次封锁 两段锁 数据库遵循的是两段锁协议,将事务分成两个阶段,加锁阶段和解锁阶段(所以叫两段锁) ...
- Windows Server体验之安装
微软在Windows Server家族中有一个新的家族,名字就是Windows Server.这个按半年频道更新的版本目前是1903和Windows 10的命名方式一样.这个产品就是以前的服务器核心安 ...
- DFS文件夹无法访问
最近DFS的文件服务器出现了部分文件和文件夹无法访问的情况.客户端直接访问DFS成员的共享文件夹时有是会出现Element not found的错误.有时打开文件的时候会出现文件不存在,或者你没有权限 ...
- 1.通俗易懂理解Kubernetes核心组件及原理
文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247483736&idx=1&sn=0cbc3d6a ...
- redhat替换yum源时redhat.repo无法删除或禁用的问题
rhel7.3系统,在替换自带的repo源时发现无论是将redhat.repo重命名还是删除,在执行yum命令后总是自动又生成redhat.repo得问题,导致替换的CentOS-Base.repo, ...
- 打印 Logger 日志时,需不需要再封装一下工具类?
在开发过程中,打印日志是必不可少的,因为日志关乎于应用的问题排查.应用监控等.现在打印日志一般都是使用 slf4j,因为使用日志门面,有助于打印方式统一,即使后面更换日志框架,也非常方便.在 < ...
- HashMap底层原理及jdk1.8源码解读
一.前言 写在前面:小编码字收集资料花了一天的时间整理出来,对你有帮助一键三连走一波哈,谢谢啦!! HashMap在我们日常开发中可谓经常遇到,HashMap 源码和底层原理在现在面试中是必问的.所以 ...
- [题解] Codeforces 1349 D Slime and Biscuits 概率,推式子,DP,解方程
题目 神题.很多东西都不知道是怎么凑出来的,随意设置几个变量,之间就产生了密切的关系.下次碰到这种题应该还是不会做罢. 令\(E_x\)为最后结束时所有的饼干都在第x个人手中的概率*时间的和.\(an ...
- 移动端300ms延迟问题和点击穿透问题
一.移动端300ms延迟问题: 一般情况下,如果没有经过特殊处理,移动端浏览器在派发点击事件的时候,通常会出现300ms左右的延迟.也就是说,当我们点击页面的时候移动端浏览器并不是立即作出反应,而是会 ...
- 从 Paxos 到 ZooKeeper
分布式一致性 分布式文件系统.缓存系统和数据库等大型分布式存储系统中,分布式一致性都是一个重要的问题. 什么是分布式一致性?分布式一致性分为哪些类型?分布式系统达到一致性后将会是一个什么样的状态? 如 ...