黑马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预留空间
*/


相关文章
|
4月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
115 2
|
4月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
221 73
|
4月前
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
166 3
|
5月前
|
存储 缓存 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 的奥秘,从入门到高效编程
|
5月前
|
算法 编译器 C++
模拟实现c++中的vector模版
模拟实现c++中的vector模版
|
5月前
|
存储 算法 C++
【c++丨STL】priority_queue(优先级队列)的使用与模拟实现
本文介绍了STL中的容器适配器`priority_queue`(优先级队列)。`priority_queue`根据严格的弱排序标准设计,确保其第一个元素始终是最大元素。它底层使用堆结构实现,支持大堆和小堆,默认为大堆。常用操作包括构造函数、`empty`、`size`、`top`、`push`、`pop`和`swap`等。我们还模拟实现了`priority_queue`,通过仿函数控制堆的类型,并调用封装容器的接口实现功能。最后,感谢大家的支持与关注。
216 1
|
6月前
|
C++ 容器
【c++丨STL】stack和queue的使用及模拟实现
本文介绍了STL中的两个重要容器适配器:栈(stack)和队列(queue)。容器适配器是在已有容器基础上添加新特性或功能的结构,如栈基于顺序表或链表限制操作实现。文章详细讲解了stack和queue的主要成员函数(empty、size、top/front/back、push/pop、swap),并提供了使用示例和模拟实现代码。通过这些内容,读者可以更好地理解这两种数据结构的工作原理及其实现方法。最后,作者鼓励读者点赞支持。 总结:本文深入浅出地讲解了STL中stack和queue的使用方法及其模拟实现,帮助读者掌握这两种容器适配器的特性和应用场景。
141 21
|
5月前
|
存储 算法 C++
深入浅出 C++ STL:解锁高效编程的秘密武器
C++ 标准模板库(STL)是现代 C++ 的核心部分之一,为开发者提供了丰富的预定义数据结构和算法,极大地提升了编程效率和代码的可读性。理解和掌握 STL 对于 C++ 开发者来说至关重要。以下是对 STL 的详细介绍,涵盖其基础知识、发展历史、核心组件、重要性和学习方法。
|
7月前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
135 1
|
1月前
|
Docker 容器
Docker网关冲突导致容器启动网络异常解决方案
当执行`docker-compose up`命令时,服务器网络可能因Docker创建新网桥导致IP段冲突而中断。原因是Docker默认的docker0网卡(172.17.0.1/16)与宿主机网络地址段重叠,引发路由异常。解决方法为修改docker0地址段,通过配置`/etc/docker/daemon.json`调整为非冲突段(如192.168.200.1/24),并重启服务。同时,在`docker-compose.yml`中指定网络模式为`bridge`,最后通过检查docker0地址、网络接口列表及测试容器启动验证修复效果。