C++中vector容器详解

1.vector容器的简介和使用方法

1.1 vector容器简介

在这里插入图片描述

vector是大小可变的数组,采用连续的存储空间,可以采用下标对vector的元素进行访问。和数组一样高效,但是又不像数组,它的大小是可以动态改变的,而且它的大小会被容器自动处理。vector使用动态分配数组来存储它的元素。当新元素插入时候,分配一个新的数组,然后将全部元素移到这个数组。vector在访问元素的时候更加高效,在末尾添加和删除元素相对高效。对于其它不在末尾的删除和插入操作,效率更低。
如图,vector容器一端是开口的,一端是封闭的。

1.2 vector容器的使用注意事项

在使用vector容器的时候要加上头文件#include<vector>
stl中的容器需要和各自的迭代器索配合才能够和数据进行操作
至于迭代器怎么使用会在下面的内容中得以展现

1.2.1 vector的构造函数

1.vector<type> v; // 默认构造函数,其中type指的是各个数据类型(int,float等等)

2.vector(v.begin(),v.end());  //将v.begin()至v.end() (左闭右开)的内容拷贝给自身

3.vector(n,elem);//将n个elem值拷贝给自身

4.vector(const vector &v);//拷贝构造函数

为了更加直观的了解 我会用最简单的代码展现这些构造

void text01()
{
 vector<int> v1;      //最简单的构造函数
    v1.push_back(10);
    v1.push_back(20);   //运用了push_back函数,在下面会有讲到
    v1.push_back(30);   //该函数的作用是在容器的末尾向里面增加元素
    v1.push_back(40);
    v1.push_back(50); 
 vector<int> v2(v1.begin(), v1.end());  //v.begin()是第一个元素 v.end()是最后一个元素
 vector<int> v3(5, 5);                  //把5个5赋给自身
 vector<int> v4(v3);                    //把v3的值进行拷贝后赋给自身
}

1.2.2 vector的赋值操作

1.vector& operator=(const vector &v); //重载赋值运算符

2.assign(v.begin(),v.end()); //将[v.begin(),v.end())区间中的元素赋值给本身

3.assign(n,elem); //将n个elem赋值给本身

1.2.3 vector的容量,大小操作

1. empy(); //判断容器是否为空
2. capacity(); //容器的容量
3. size();  //返回容器中元素的个数
4. resize(int num);  //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
                               //如果容器变短,则末尾超出容器长度的元素被删除。
5. resize(int num, elem); //重新指定容器的长度为num,若容器变成,则以elem值填充新位置。
                                       //如果容器变短,则末尾超出容器长度的元素被删除。

对于vector而言,真实的容量和其容器中的个数并不一样,就好比与水壶,一个3L的水壶,里面可以是2L的水,2.5升的水,但是对于vector而言,其容量是可以随时改变的,就如同例子中的3L,可以更具实际元素的多少进行收缩或者增长。

 #include <iostream>
 #include<vector> 
using namespace std;
//vector容器的容量和大小操作
void print(vector<int>&v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)  //此地使用了迭代器,也就是容器和
    {                                                                //元素之间的桥梁,指针从元素头遍历到尾
        cout << *it << " ";                                          //用*it的方式将其输出
    }
    cout << endl;
}
void test03()
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    print(v1);

    if (v1.empty()) //为真 代表容器为空
    {
        cout << "v1为空" << endl;
    }
    else
    {
        cout << "v1不为空:" << endl;
        cout << "capacity容量:" << v1.capacity() <<endl;
        cout << "v1的大小为:" << v1.size() << endl;
        //重新指定大小
        v1.resize(15);  //如果重新指定的比原来长了,默认用0填充新的位置
        print(v1);
        v1.resize(20,100);  //利用重载版本,参数2可以指定默认填充值
        print(v1);
        v1.resize(5);  //如果重新指定的比原来短了,超出的部分会删除掉
        print(v1);
    }
}
int main()
{
    test03();
    return 0;
}

程序结果如下:
在这里插入图片描述

1.2.4 vector的插入,删除操作

1. push_back(elem);  //末尾插入元素elem
2. pop_back();  //删除最后一个元素
3. insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
4. insert(const_iterator pos, int count ele); //迭代器指向位置pos插入count个元素
5. erase(const_iterator pos); //删除迭代器指向的元素
6. erase(cons_titerator start, const_iterator end);  //删除迭代器从start到end之间的元素
7. clear();  //删除容器中所有元素

1.2.5 vector的swap互换函数

#include <iostream>
#include<vector> 
using namespace std;
//vector容器互换
void print(vector<int>v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

}

//1、基本使用
void test01()
{
    cout << "交换前:" << endl;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }    
    print(v1);
    vector<int>v2;
    for (int i = 10; i > 0; i--)
    {
        v2.push_back(i);
    }
    print(v2);
    cout << "交换后:" << endl;
    v1.swap(v2);
    print(v1);
    print(v2);
}
int main()
{
    test01();
    return 0;
}

程序结果如下:
在这里插入图片描述

1.2.6 vector的数据存取操作

函数原型:

1. at(int idx);   ///返回索引为idx所指的数据。
2. operator[];  //返回索引idx所指的数据。
3. front();  //返回容器中第一个数据元素
4. back();   //返回容器中最后一个数据元素。

2. 点点滴滴的小补充:

在我平时刷平台的题目的时候,会遇到一些与之相关的地方,于是,我在此做一个补充内容

【补充1】sort()

  1. sort()函数是C++标准库中的排序函数,头文件为algorithm
  2. sort()函数的参数,sort(起始地址,结束地址,比较器);,其中比较器可以省略,默认升序,如图
//对数组进行排序
int arr[10]={5,3,6,0,2,8,2,6,9,11};
sort(arr,arr+10); 
//对vector进行排序
vector<int> v={5,3,6,0,2,8,2,6,9,11};
sort(v.begin(),v.end());

这里代表的就是把arr里面的内容进行一个升序的排序

【补充2】numeric头文件中的accumulate函数

基本用法:求vector nums的元素累计和、累计减、累计乘、累计除

 #include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(int, char**) {
    vector<int> nums = { 6,3,2 };
    // 默认求和,输出为:0+6+3+2 = 11
    cout << accumulate(nums.begin(), nums.end(), 0) << endl;
    // 初始值为1求和,输出为:1+6+3+2 = 12
    cout << accumulate(nums.begin(), nums.end(), 1) << endl;
    // 求累减,输出为:0-6-3-2 = -11
    cout << accumulate(nums.begin(), nums.end(), 0, minus<int>()) << endl;
    // 求累乘,输出为:1*6*3*2 = 36
    cout << accumulate(nums.begin(), nums.end(), 1, multiplies<int>()) << endl;
    // 求累除,输出为:36/6/3/2 = 1
    cout << accumulate(nums.begin(), nums.end(), 36, divides<int>()) << endl;
    return 0;
}

关于accumulate中参数的使用情况如下:

accumulate(起始迭代器, 结束迭代器, 初始值, 自定义操作函数)
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
    vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = accumulate(arr.begin(), arr.end(), 0); // 初值0 + (1 + 2 + 3 + 4 +... + 10)
    cout << sum << endl;	// 输出55
    return 0;
}

最后的一些些话

身为一个大二的新手菜狗,深感计算机内容之多,路途之艰辛,望共勉之。

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
VectorC++一个非常有用的容器类,它可以动态地调整存储空间大小。下面是一些常见的使用方法: 1. 创建Vector 使用Vector时,需要包含头文件<vector>。创建一个空的Vector对象可以使用默认构造函数: ```c++ #include <vector> using namespace std; vector<int> vec; //创建一个空的vector ``` 也可以使用初始化列表来创建一个包含元素的Vector: ```c++ vector<int> vec = {1, 2, 3, 4, 5}; //创建包含5个元素的vector ``` 2. 添加元素 向Vector添加元素使用push_back()方法,可以添加任意类型的元素,例如: ```c++ vec.push_back(6); //向Vector添加一个整数 vec.push_back("hello"); //向Vector添加一个字符串 ``` 3. 访问元素 可以使用下标操作符[]或at()方法来访问Vector的元素,例如: ```c++ int a = vec[0]; //获取Vector第一个元素 int b = vec.at(1); //获取Vector第二个元素 ``` 4. 删除元素 使用erase()方法可以删除Vector的元素,例如: ```c++ vec.erase(vec.begin() + 2); //删除Vector索引为2的元素 ``` 5. 获取Vector大小 使用size()方法可以获取Vector元素的数量,例如: ```c++ int size = vec.size(); //获取Vector元素的数量 ``` 6. 获取Vector容量 使用capacity()方法可以获取Vector的容量,即Vector可以存储的元素数量,例如: ```c++ int capacity = vec.capacity(); //获取Vector的容量 ``` 7. 清空Vector 使用clear()方法可以清空Vector的所有元素,例如: ```c++ vec.clear(); //清空Vector的所有元素 ``` 以上是Vector的一些常见用法,还有很多其他的用法可以参考C++官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值