vector类(上)

简介: vector类(上)

> 作者简介:დ旧言~,目前大二,现在学习Java,c,c++,Python等

> 座右铭:松树千年终是朽,槿花一日自为荣。

> 目标:熟悉vector库

> 毒鸡汤:从人生低谷走上人生巅峰是一条很漫长,一开始会很累,但是慢慢就会习惯这种感觉,这说明你在不断的成长。

> 望小伙伴们点赞👍收藏✨加关注哟💕💕



🌟前言

       相信看完博主的string类CSDN,学习vector会很轻松的🤭🤭,咱们依旧参考c++的官网:cplusplus.com - The C++ Resources Network



⭐主体

       这里就需要浅谈一下什么vector,基于在数组的问题,当我们创建一个数组时,此时元素的类型已经锁定,而vector就可以很好的解决这个问题。当然vector不仅仅是这些用法,那咱们就看看vector有啥子东西值得我们学习。


       咱们的主题部分还是按照官网的分类来,具体分为下面这些板块:Member functions(成员函数),Iterators(迭代器),Capacity(容量),Element access(元素访问),Modifiers(修改器),Non-member function (非成员函数)☺️☺️☺️。



🌙Member functions(成员函数)

每一个接口的成员函数都很重要,无论是拷贝构造还是赋值运算重载,都贯彻c++。



💫constructor(构造函数)

根据使用的构造函数版本初始化其值。



在vector的构造函数中有许多的模板,那我们看看到底有哪些类型模板🧐🧐。


1️⃣

这种构造函数本质上是提供一种无参构造

default (1)  explicit vector (const allocator_type& alloc = allocator_type());


咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
  vector<char> v1;
  for (auto ch : v1)
  {
    cout << ch << endl;
  }
  return 0;
}


运行结果:


2️⃣

这种构造函数本质上是提供一个类模板

fill (2)  
explicit vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type());


  • 💦size_type n:无符号整形
  • 💦const value_type& val = value_type():模板参数类型
  • 💦const allocator_type& alloc = allocator_type():const分配器类型


第三个参数咱们不讲解,只需懂得前面两个参数就行。


#include<iostream>
#include<vector>
using namespace std;
int main()
{
  int a = int();
  double b = double();
  char c = char();
  cout << a << endl;
  cout << b << endl;
  printf("%d", c);
  return 0;
}


运行结果:



解析:

在c++当中给每个自定义类型也创建了对应的默认构造函数,其构造的默认结果为0。


3️⃣

这种构造函数本质上是提供一个迭代器

range (3) 
template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());


  • 💦第一个参数迭代器开始的地方
  • 💦第二个参数就是迭代器结束的地方
  • 💦第三个参数const分配器类型


第三个参数咱们不讲解,只需懂得前面两个参数就行。

咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
  vector<int> v{ 1, 2, 3,4 };
  vector<int> v1(v.begin(), v.end());
  for (auto ch : v1)
  {
    cout << ch << endl;
  }
  return 0;
}


运行结果:



4️⃣

这种构造函数本质上就是拷贝构造,创建一个对象的时候将另外一个对象的内容初始化给新创建的对象

copy (4)  vector (const vector& x);


咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
  vector<int> v1{ 1,2,3,4,5 };
  vector<int>v2(v1);
  for (auto ch : v2)
  {
    cout << ch << endl;
  }
  return 0;
}


运行结果:



💫operator(赋值重载)

这个就是赋值重载,将一个vector对象的内容赋值给另一个vector对象



咱们看看它的使用:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<int> v1{ 1,2,3,4,5 };
  vector<int>v2(v1);
 
  v2 = v1;
  for (auto ch : v2)
  {
    cout << ch << endl;
  }
  return 0;
}


运行结果:



🌙Iterators(迭代器)

       我们已经在string类中讲解这个迭代器,在vector中使用迭代器基本和string类一样,这里我们就加加速,搞快点😏😏。


💫利用begin和end实现迭代器

  • begin()函数返回的是vector的首位置
  • end()函数返回的是vector最后一个位置(即最后一个元素的下一个位置)




咱看看代码:

#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> v1{ 1,2,3,4,5 };
    vector<int>::iterator it = v1.begin();
 
    while (it != v1.end())
    {
        cout << *it;
        ++it;
    }
 
    cout << endl;
    return 0;
}


运行结果:



💫范围for实现迭代器

不知道小伙伴还记得auto关键字不,auto可以推导出元素属性(int,char)


咱们看看代码:

#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> v1{ 1,2,3,4,5 };
    for (auto ch : v1)
    {
        cout << ch;
    }
    cout << endl;
    return 0;
}


运行结果:



💫反向迭代器

这里需要介绍一下rbegin()和rend()这两个函数,这两个函数是用来配合反向迭代器使用的。


  • rbegin()函数返回的是vector的最后一个有效元素
  • rend()函数返回的是vector的第一个元素的前一个位置。




咱们看看代码:

#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> v1{ 1,2,3,4,5 };
    vector<int>::reverse_iterator rit = v1.rbegin();
    while (rit != v1.rend())
    {
        cout << *rit;
        rit++;
    }
    cout << endl;
    return 0;
}


运行结果:



💫const修饰的迭代器

const修饰的迭代器是不可以改变的(只能读不能写)


咱们看看代码:

#include <iostream>
#include <vector>
 
using namespace std;
void Func(const vector<int> v)
{
    vector<int>::const_iterator cit = v.begin();
    // 读操作
    while (cit != v.end())
    {
        cout << *cit;
        cit++;
    }
    cout << endl;
 
    // 不能进行写操作,会报错
    // cit = s.begin();
    // while (cit != s.end())
    // {
    //     (*cit) += 1;
    //     cout << *cit;
    //     cit++;
    // }
}



vector类(下)           https://developer.aliyun.com/article/1565455

目录
相关文章
|
5月前
|
安全 Java uml
|
3月前
|
Serverless C++ 容器
vector类(下)
vector类(下)
41 0
|
5月前
|
存储 安全 Java
ArrayList和Vector及Vector的区别
综上所述,选择ArrayList、Vector还是LinkedList取决于您的具体需求。如果需要高性能、随机访问元素,且不需要考虑线程安全,ArrayList是一个不错的选择。如果需要线程安全,可以考虑使用Vector,但需要注意性能问题。如果需要频繁插入和删除元素,LinkedList可能更适合。
50 3
|
10月前
|
C++ 容器
【C++】STL之vector类模拟-2
【C++】STL之vector类模拟
34 0
|
10月前
|
安全 C++ 容器
【C++】STL之vector类模拟-1
【C++】STL之vector类模拟
32 0
|
10月前
|
Linux 编译器 测试技术
【C++】STL之vector类模拟-3
【C++】STL之vector类模拟
32 0
[VC6]std::vector派生类无法调用std::vector的解决方法
[VC6]std::vector派生类无法调用std::vector的解决方法
|
C++ 容器
【C++】vector中的常见函数和使用
【C++】vector中的常见函数和使用
55 0
|
存储 算法 测试技术
vector的常用接口
这里我们讲解vector的时候就不会像string类一样这么详细,string类讲的详解一些,为后面做铺垫。有了string类的基础,大家看一些接口就知道是什么意思,这里给大家而是讲解一些常用的接口,剩下的接口不太常用,如果大家遇到了,查文档即可,这里推荐两个C++文档,cplusplus.com 以及 cppreference.com。第二个网站是C++的官网,但是感觉不太好用,我平时都喜欢用第一个,感觉较为方便。
 vector的常用接口
|
存储 C++ 容器