黑马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++中的vector模版
模拟实现c++中的vector模版
|
存储 缓存 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++丨STL】vector模拟实现
本文深入探讨了 `vector` 的底层实现原理,并尝试模拟实现其结构及常用接口。首先介绍了 `vector` 的底层是动态顺序表,使用三个迭代器(指针)来维护数组,分别为 `start`、`finish` 和 `end_of_storage`。接着详细讲解了如何实现 `vector` 的各种构造函数、析构函数、容量接口、迭代器接口、插入和删除操作等。最后提供了完整的模拟实现代码,帮助读者更好地理解和掌握 `vector` 的实现细节。
345 0
|
存储 编译器 C语言
【c++丨STL】vector的使用
本文介绍了C++ STL中的`vector`容器,包括其基本概念、主要接口及其使用方法。`vector`是一种动态数组,能够根据需要自动调整大小,提供了丰富的操作接口,如增删查改等。文章详细解释了`vector`的构造函数、赋值运算符、容量接口、迭代器接口、元素访问接口以及一些常用的增删操作函数。最后,还展示了如何使用`vector`创建字符串数组,体现了`vector`在实际编程中的灵活性和实用性。
848 5
|
存储 对象存储 C++
C++ 中 std::array<int, array_size> 与 std::vector<int> 的深入对比
本文深入对比了 C++ 标准库中的 `std::array` 和 `std::vector`,从内存管理、性能、功能特性、使用场景等方面详细分析了两者的差异。`std::array` 适合固定大小的数据和高性能需求,而 `std::vector` 则提供了动态调整大小的灵活性,适用于数据量不确定或需要频繁操作的场景。选择合适的容器可以提高代码的效率和可靠性。
|
存储 C++ 索引
【C++打怪之路Lv9】-- vector
【C++打怪之路Lv9】-- vector
383 1
|
算法 C++ 容器
C++之打造my vector篇(下)
C++之打造my vector篇(下)
|
存储 编译器 C++
C++之打造my vector篇(上)
C++之打造my vector篇(上)
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
11月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
447 12