黑马c++ STL部分 笔记(3) vector容器

简介: 黑马c++ STL部分 笔记(3) vector容器

vector可以动态扩展(不是在原有基础上扩展,而是找更大空间,然后将元数据拷贝新空间,释放原空间)

544ba34ff35a4c03b9be8e1ebe50f6a4.png

  • vector容器的迭代器是支持随机访问的迭代器

1. vector容器的构造

// vector容器的构造(一般用拷贝构造)
/*
vector<T> v; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem); //构造函数将n个elem拷贝给本身。
vector(const vector &vec); //拷贝构造函数。
*/
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  vector<int> v1; // 默认构造
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i);
  }
  printvector(v1);
  vector<int> v2(v1.begin(), v1.end()); // 利用区间方式进行构造
  printvector(v2);
  vector<int> v3(3, 100);
  printvector(v3); // n个element赋值
  vector<int> v4(v3);
  printvector(v4); // 拷贝构造
}
 
int main()
{
  test01();
}


2. vector容器的赋值操作

// vector容器的赋值操作(一般用=)
/*
vector& operator=(const vector &vec);//重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
*/
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  vector<int> v1;
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i);
  }
  printvector(v1);
  vector<int> v2;
  v2 = v1; //=方式
  printvector(v2);
  vector<int> v3;
  v3.assign(v1.begin(), v1.end()); // assign方式
  printvector(v3);
  vector<int> v4;    // 构造
  v4.assign(3, 100); // 赋值:n个element方式
  printvector(v4);
}
 
int main()
{
  test01();
}


3.vector容量和大小

// vector容量和大小
/*
empty(); //判断容器是否为空
capacity(); //容器的容量
size(); //返回容器中元素的个数 capacity>=size
resize(int num); //重新指定容器的长度为num
若容器变长,则以默认值0填充新位置。
若容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem); //重新指定容器的长度为num
若容器变长,则以elem值填充新位置。
若容器变短,则末尾超出容器长度的元素被删除
*/
 
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  vector<int> v1;
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i);
  }
  printvector(v1);
  // 判空
  if (v1.empty())
  {
    cout << "v1为空" << endl;
  }
  else
  {
    cout << "v1不为空" << endl;
    cout << "v1容量为" << v1.capacity() << endl; // 16
    cout << "v1大小为" << v1.size() << endl;     // 10
  }
  // 重新指定大小
  v1.resize(20);     // 默认用0填充
  printvector(v1);   // 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0
  v1.resize(21, 99); // 指定用99填充
  printvector(v1);   // 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 99
  v1.resize(5);
  printvector(v1); // 0 1 2 3 4
}
 
int main()
{
  test01();
}
/*
总结:
判断是否为空 — empty
返回元素个数 — size
返回容器容量 — capacity
重新指定大小 — resize
*/


4.vector插入和删除

// 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); //删除迭代器指向的元素
erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
clear(); //删除容器中所有元素
*/
 
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  vector<int> v1;
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i); // 尾插法
  }
  printvector(v1); // 0 1 2 3 4 5 6 7 8 9
  v1.pop_back();   // 尾删
  printvector(v1); // 0 1 2 3 4 5 6 7 8
  // 插入insert
  v1.insert(v1.begin(), 100);     // 指定位置插入数据
  printvector(v1);                // 100 0 1 2 3 4 5 6 7 8
  v1.insert(v1.begin(), 2, 1000); // 指定位置插入n个element
  printvector(v1);                // 1000 1000 100 0 1 2 3 4 5 6 7 8
  // 删除erase
  v1.erase(v1.begin());                 // 删除指定位置元素
  printvector(v1);                      // 1000 100 0 1 2 3 4 5 6 7 8
  v1.erase(v1.begin(), v1.begin() + 1); // 删除区间元素(左闭右开)
  printvector(v1);                      // 100 0 1 2 3 4 5 6 7 8
  // 清空clear
  v1.clear();
  printvector(v1);
}
 
int main()
{
  test01();
}
/*
总结:
尾插 — push_back
尾删 — pop_back
插入 — insert (位置迭代器)
删除 — erase (位置迭代器)
清空 — clear
*/


5.vector数据存取

// vector数据存取
/*
at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
*/
 
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
void test01()
{
  vector<int> v1;
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i);
  }
 
  for (int i = 0; i < v1.size(); i++) //[]方式
  {
    cout << v1[i] << " "; // 0 1 2 3 4 5 6 7 8 9
  }
  cout << endl;
  for (int i = 0; i < v1.size(); i++) // at方式
  {
    cout << v1.at(i) << " "; // 0 1 2 3 4 5 6 7 8 9
  }
  cout << endl;
  cout << v1.front() << " " << v1.back() << endl; // 获取第一个和最后一个元素 0 9
}
 
int main()
{
  test01();
}
/*
总结:
除了用迭代器获取vector容器中元素,[ ]和at也可以
front返回容器第一个元素
back返回容器最后一个元素
*/


6.vector互换容器

// vector互换容器
/*
swap(vec); // 将vec与本身的元素互换
*/
 
#include <bits/stdc++.h>
using namespace std;
void printvector(vector<int> &v)
{
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << " ";
  }
  cout << endl;
}
// 1 基本使用
void test01()
{
  vector<int> v1;
  for (int i = 0; i < 10; i++)
  {
    v1.push_back(i);
  }
  cout << "交换前" << endl;
  printvector(v1); // 0 1 2 3 4 5 6 7 8 9
  vector<int> v2;
  for (int i = 9; i >= 0; i--)
  {
    v2.push_back(i);
  }
  printvector(v2); // 9 8 7 6 5 4 3 2 1 0
  cout << "交换后" << endl;
  v1.swap(v2);
  printvector(v1); // 9 8 7 6 5 4 3 2 1 0
  printvector(v2); // 0 1 2 3 4 5 6 7 8 9
}
// 2 实际用途:巧用swap可以收缩内存空间
void test02()
{
  vector<int> v;
  for (int i = 0; i < 100000; i++)
  {
    v.push_back(i);
  }
  cout << "v的容量:" << v.capacity() << endl; // 131072
  cout << "v的大小:" << v.size() << endl;     // 100000
  v.resize(3);
  cout << "v的容量:" << v.capacity() << endl; // 131072
  cout << "v的大小:" << v.size() << endl;     // 3
  // 巧用swap收缩内存空间,不会浪费空间
  vector<int>(v).swap(v);                     // vector<int>(v)匿名对象
  cout << "v的容量:" << v.capacity() << endl; // 3
  cout << "v的大小:" << v.size() << endl;     // 3
}
 
int main()
{
  test01();
  test02();
}
/*
总结:swap可以使两个容器互换,可以达到实用的收缩内存效果
*/


7.vector预留空间

// vector预留空间
/*
功能描述:
减少vector在动态扩展容量时的扩展次数
函数原型:
reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
*/
 
#include <bits/stdc++.h>
using namespace std;
void test01()
{
  vector<int> v1;
  int num1 = 0; // 统计开辟的次数
  int *p1 = NULL;
  for (int i = 0; i < 100000; i++)
  {
    v1.push_back(i);
    if (p1 != &v1[0])
    {
      p1 = &v1[0]; // 若更换了内存,则p!=v首地址
      num1++;
    }
  }
  cout << num1 << endl; // 18
                        // 利用reservr预留空间
  vector<int> v2;
  v2.reserve(100000);
  int num2 = 0; // 统计开辟的次数
  int *p2 = NULL;
  for (int i = 0; i < 100000; i++)
  {
    v2.push_back(i);
    if (p2 != &v2[0])
    {
      p2 = &v2[0]; // 若更换了内存,则p!=v首地址
      num2++;
    }
  }
  cout << num2 << endl; // 1
}
 
int main()
{
  test01();
}
/*
总结:如果数据量较大,可以一开始利用reserve预留空间
*/


相关文章
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
291 9
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
308 5
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
297 2
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
226 0
|
C++ 容器
【C/C++笔记】迭代器
【C/C++笔记】迭代器
264 1
|
设计模式 存储 C++
【C++】C++ STL探索:容器适配器 Stack 与 Queue 的使用及模拟实现(二)
【C++】C++ STL探索:容器适配器 Stack 与 Queue 的使用及模拟实现
173 0
|
存储 C++ 容器
【C++】C++ STL探索:容器适配器 Stack 与 Queue 的使用及模拟实现(一)
【C++】C++ STL探索:容器适配器 Stack 与 Queue 的使用及模拟实现
203 0
|
存储 安全 程序员
【C/C++笔记】迭代器范围
【C/C++笔记】迭代器范围
269 0
|
7月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1112 109