1.vector与array的对比

vector是stl中常用的容器之一。vector与数组array比较类似,可以看成是array功能更强大的升级版。与array的不同在于,array是静态数组,容量大小在数组初始化的时候就是固定的。而vector是一个动态数组,即vector可以动态调整数组大小而无须用户进行手动干预。

因为vector的底层实现是数组,所以其优势是在尾部插入删除数据很快,随机访问也很快,时间复杂度只有O(1)。而当其在头部或者其他位置插入删除数据时,因为需要对元素进行移动复制,所以效率会很低。

2.vector初始化

vector的初始化有如下几种方式
1.vector<int> first first为空,size为0, 此时first中没有元素,capacity也为0,说明此时 还没有具体分配内存空间。这种情况适合元素个数未知需要动态添加元素的场景。
2

vector<int> second(first)
vector<int> second = first

上面两种方式都可以实现初始化,其中second是first拷贝,且类型必须相同,second中元素个数与类型与first中保持一致。

3 vector<int> third = {1, 2, 3, 4, 5}
4 vector<int> fourth(third.begin(), third.begin()+4) 使用两个迭代器范围内元素进行拷贝,范围中的元素类型与新容器中的元素类型须保持一致。这种方法对于获得一个序列的子序列比较方便。

5.vector<int> fifth(3)
上面的方式使用默认值初始化,每个元素的值使用缺省默认值。对于int类型,默认值为0.

6.vector<int> sixth(3, -1);
与第五种方式的区别在于,该方式指定了初始化的值。

3.vector初始化代码实测

void print_vec(vector<int> &v, const char *p) {cout<<p<<" is: ";for(int ele: v) {cout<<ele<<" ";}cout<<endl;
}void init_vec() {vector<int> first;vector<int> second(first);second = {1, 2, 3};print_vec(first, "first");print_vec(second, "second");vector<int> third = {1, 2, 3, 4, 5};vector<int> fourth(third.begin(), third.begin()+4);print_vec(fourth, "fourth");vector<int> fifth(3);vector<int> sixth(3, -1);print_vec(fifth, "fifth");print_vec(sixth, "sixth");
}

上述代码在main方法中跑起来,最后输出结果如下

first is:
second is: 1 2 3
fourth is: 1 2 3 4
fifth is: 0 0 0
sixth is: -1 -1 -1

4.list与vector区别

前面我们讲到vector与数组类似,而数组是具有一段连续的内存空间的,并且起始的地址不变。正因为这个特性,其才能高效地进行随机访问,并高效在尾部进行追加,时间复杂度均为O(1)。
而stl中的list是由双向链表实现,因此其内存空间并不连续。因为内存空间不连续,只能通过指针去访问数据,因此list不能随机读取,追加数据效率也没有vector效率高。但因为其是指针结果,随机插入数据与删除数据就特别方便,并且可以在两端进行pop与push操作。

5.list初始化

void print_list(list<int> ll, const char *p) {cout<<p<<" is: ";for(int val: ll) {cout<<val<<" ";}cout<<endl;
}void init_list() {list<int> first;list<int> second(4, -1);list<int> third(second.begin(), second.end());list<int> fourth(third);int nums[] = {1, 2, 3, 4, 5};list<int> fifth(nums, nums + sizeof(nums) / sizeof(nums[0]));print_list(third, "third");print_list(fourth, "fourth");print_list(fifth, "fifth");
}

list初始化的方式与vector类似。上面方法在main方法中跑起来,最后结果为

third is: -1 -1 -1 -1
fourth is: -1 -1 -1 -1
fifth is: 1 2 3 4 5

6.代码实测vector与list对比

前面我们提到vector与list的区别:
vector方便尾部追加与随机访问,但是随机插入与删除比较耗时。
list擅长随机插入与删除,但是随机访问与尾部追加没有vector快。
下面我们就用代码来进行实测,看看是不是确实我们所说。

#include<iostream>
#include<vector>
#include<list>
#include<ctime>
using namespace std;void vec_list_compare() {clock_t start, end, start2, end2, start3, end3, start4, end4, start5, end5, start6, end6;int n = 100000;vector<int> vec;list<int> ll;//  尾部添加元素,vector可以高效随机读取start = clock();for(int i=0; i<n; i++) {vec.push_back(i);}end = clock();cout<<"vec add data cost time is: "<<double(end-start)/CLOCKS_PER_SEC<<endl;start2 = clock();for(int i=0; i<n; i++) {ll.push_back(i);}end2 = clock();cout<<"list add data cost time is: "<<double(end2-start2)/CLOCKS_PER_SEC<<endl;cout<<endl;// 头部删除元素,list可以高效删除start3 = clock();for(vector<int>::iterator it=vec.begin(); it!=vec.end();it++) {vec.erase(it);}end3 = clock();cout<<"vec delete cost time is: "<<double(end3-start3)/CLOCKS_PER_SEC<<endl;start4 = clock();while(ll.size() > 0) {ll.pop_back();}end4 = clock();cout<<"list delete cost time is: "<<double(end4-start4)/CLOCKS_PER_SEC<<endl;cout<<endl;// 中间位置插入数据,list效率高start5 = clock();for(int i=0; i<10000; i++) {vector<int>::iterator it = find(vec.begin(), vec.end(), n/2);vec.insert(it, n);}end5 = clock();cout<<"vec insert cost time is: "<<double(end5-start5)/CLOCKS_PER_SEC<<endl;start6 = clock();for(int i=0; i<10000; i++) {ll.insert(ll.begin(), n);}end6 = clock();cout<<"list insert cost time is: "<<double(end6-start6)/CLOCKS_PER_SEC<<endl;
}int main(int argc, char const *argv[])
{vec_list_compare();return 0;
}

上述代码运行起来,输出结果为

vec add data cost time is: 0.006538
list add data cost time is: 0.027703vec delete cost time is: 0.51943
list delete cost time is: 0.015977vec insert cost time is: 7.70983
list insert cost time is: 0.002979

可以看到,在尾部追加数据,vector类型完胜。
而对于随机删除数据与随机插入数据,list则碾压vector。

stl vector与list详细对比相关推荐

  1. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

  2. r语言和python-r语言和python的详细对比

    r语言和python的对比,两者各有千秋,究竟要学哪一个,可根据自己的实际需求来作出选择,当然,最好是两者都学. 01 开发目的 R语言 R是由统计学家开发的,它的出生就肩负着统计分析.绘图.数据挖掘 ...

  3. 【转】SVN 与 GIT 详细对比

    [转]SVN 与 GIT 详细对比 git和svn的详细对比 近期就[版本管理工具是否进行切换SVN->Git]的问题进行了讨论,于是对svn和Git进行了相关研究,进而梳理出Git的特点(优. ...

  4. STL vector的几种清空容器(删除)办法

    1.为什么需要主动释放vector内存 来自 <https://blog.csdn.net/hellokandy/article/details/78500067> vector其中一个特 ...

  5. STL vector 容器介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  6. STL vector的erase操作问题

    STL vector的erase操作问题 一老大说CSDN上有篇博文("关于STL vector的erase操作",地址是:http://blog.csdn.net/tingya/ ...

  7. STL vector容器

    介绍  这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用. ...

  8. Mybatis与Hibernate的详细对比

    转载自  Mybatis与Hibernate的详细对比 前言 这篇博文我们重点分析一下Mybatis与Hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate ...

  9. stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素...

    stl vector 函数 打印向量的所有元素 (Printing all elements of a vector) To print all elements of a vector, we ca ...

  10. stl vector 函数_vector :: at()函数以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: at()函数 (C++ vector::at() function) vector::at() is a library function o ...

最新文章

  1. python中filter函数的使用
  2. jQuery之动画效果
  3. 进程和线程的一些见解
  4. Tcl与Design Compiler (三)——DC综合的流程
  5. html5+开发window桌面图标,js模仿windows桌面图标排列算法具体实现(附图)
  6. gVIM+zencoding快速开发HTML/CSS/JS(适用WEB前端)
  7. 连接excel执行Insert Into语句出现“操作必须使用一个可更新的查询”的解决
  8. navicat fo mysql 教程_Navicat For MySQL的简单使用教程
  9. MySQL数据技术嘉年华,带你深入MySQL的世界
  10. 语言中的petchar运用_自闭症儿童语言障碍家庭训练,需要融入这些方法
  11. gitlab汉化及关联LDAP
  12. 如何拍好运动风人像?
  13. 计算机英语900句.pdf,计算机英语900句第一章第一课:概貌
  14. java 释放锁,在Java中以原子方式释放多个锁
  15. Systemview5安装的问题:the license for this version of systemview has expired......
  16. c语言双向循环链表合并,双向循环链表的合并
  17. 西安翼迅网络的破解尝试
  18. IP签名档PHP源码,IPCard 一款天气图标签名档源码
  19. Python 高效提取 HTML 文本的方法
  20. Day3-T31项目 异常处理与日志——2021-11-02

热门文章

  1. unity 录制游戏内视频(1)
  2. 管理软raid磁盘队列
  3. 软考网络工程师--数据通信基础
  4. KVC 与 KVO 理解
  5. xcode经验汇总(持续更新中)
  6. html文本框(input)不保存缓存记录
  7. CISCO AP安裝方法
  8. win10去掉文件夹前面的复选框
  9. JavaSE中容易忽视的坑
  10. 第六届中国电子信息博览会今日正式开幕,智享新时代!