首页 > 其他分享 >vector 和 list的区别

vector 和 list的区别

时间:2024-01-23 09:46:55浏览次数:22  
标签:sort cout iterator 区别 list back vector continer include

vector list
定义 动态数组 双向链表
增加 push_back push_back, push_front, insert
删除 pop_back pop_back, pop_front, remove
排序 头文件 sort 成员函数 sort

list经过排序、插入操作后,迭代器不会失效,仍指向原来的元素

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;

template<typename T>
void print(const T& continer){
    for(auto i = continer.begin(); i != continer.end(); ++i){
        cout << *i << " ";
    }
    cout << endl;
}

int main(){
    list<int> a {9, 8, 0, 7};
    cout << "origin: ";
    print(a);

    cout << "after insert: ";
    auto it = a.begin();
    a.push_front(10);
    print(a);
    cout << "iterator it: " << *it << endl;

    a.sort(); 
    cout << "after sort: ";
    print(a);
    cout << "iterator it: " << *it << endl;
    return 0;
}

output

origin: 9 8 0 7 
after insert: 10 9 8 0 7 
iterator it: 9
after sort: 0 7 8 9 10 
iterator it: 9

而vector不满足这样的情况

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;

template<typename T>
void print(const T& continer){
    for(auto i = continer.begin(); i != continer.end(); ++i){
        cout << *i << " ";
    }
    cout << endl;
}


int main(){
    vector<int> a {9, 8, 0, 7};
    cout << "origin: ";
    print(a);

    cout << "after insert: ";
    auto it = a.begin();
    a.insert(a.begin() ,10);
    print(a);
    cout << "iterator it: " << *it << endl;

    sort(a.begin(), a.end());
    cout << "after sort: ";
    print(a);
    cout << "iterator it: " << *it << endl;    
    return 0;
}

output

origin: 9 8 0 7 
after insert: 10 9 8 0 7 
iterator it: 0
after sort: 0 7 8 9 10 
iterator it: 0

标签:sort,cout,iterator,区别,list,back,vector,continer,include
From: https://www.cnblogs.com/dctwan/p/17981657

相关文章

  • [转]js 正则相关的两个方法match和exec的区别
    原文地址:jsmatch和exec的区别_javascriptmatchexec-CSDN博客一、使用方法不同exec()是正则对象的方法;reg.exec(str);match()是字符串的正则表达方法;str.match(reg);二、返回结果不同w3cSchool是这么说的:match()方法将检索字符串stringObject,以找到一个或多个与regex......
  • 字符串转化为toList
    certNameList="certNameList":"消防设施工程专业承包二级,地基基础工程专业承包一级,电子与智能化工程专业承包一级,建筑装修装饰工程专业承包一级,建筑幕墙工程专业承包一级",for(EntQualificationVOvo:entQualificationVOList){StringcertNameList......
  • [转帖]OS、PFS、DFS 有啥区别?一文搞懂 6 大临床试验终点
    https://oncol.dxy.cn/article/670607 说到肿瘤临床研究,就不得不说临床试验终点(EndPoint),比如大家熟知的OS、PFS、ORR还有DFS、TTP、TTF……不同的终点服务于不同的研究目的。让我们一起来看看常用的临床试验终点都有什么区别以及优缺点。总生存overallsurvival,OS......
  • pip list 与 conda list 的区别
    condaconda是Anaconda发行版中的包管理工具,主要用于管理包含Python及其相关库的科学计算环境,如NumPy、SciPy、Pandas、Matplotlib等等,它支持创建和管理Python环境,可以方便地在不同的项目中使用不同的库及其版本。condalist命令用于列举当前Python环境下所有使用cond......
  • Git必知必会基础(11):merge和rebase的区别
     本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/10854115.htmlmerge和rebase使用回顾上两篇我们分别演示了merge和rebase的使用,分别详见:https://www.cnblogs.com/uncleyong/p/17967432https://www.cnblogs.com/uncleyong/p/17978213下面我们来总结下二者的差异......
  • 使用cxTreeList创建权限管理
    设计数据库结构及内容格式如下设计界面如下,打开、保存、打印这三列Properties改为CheckBox方式显示加载菜单代码如下procedureTForm1.Button1Click(Sender:TObject);varpNode:TcxTreeListNode;//父节点RootKey:string;//根节点的键值RootNode,ChildNod......
  • STL-list链表
    STL-list链表目录STL-list链表初始化创建添加删除元素遍历迭代参考函数参考资料STL-list容器,又称双向链表容器,即该容器的底层是以双向链表的形式实现的。这意味着,list容器中的元素可以分散存储在内存空间里,而不是必须存储在一整块连续的内存空间中。list容器中各个元素的......
  • STL-vector向量
    STL-vector向量目录STL-vector向量1.头文件2.构造函数3.索引存取元素4.遍历元素4.capacity相关5.插入元素6.删除元素7.排序和翻转8.底层原理9.特殊记忆函数总结参考资料vector数组是一个能存放任意数据类型(类,结构,普通变量类型等)的动态数组,在数据结构中就相当于顺序储存的线性......
  • python中path[:]跟path有什么区别?
    问题:看到别写的算法里有path[:],不知道跟普通的有什么区别?AI答案:在Python中,`result.append(path[:])`和`result.append(path)`有本质的区别。result.append(path)这种方式,是将path列表的引用加入到result列表,如果后续更改了path列表,result中的相应元素也会随之改变。......
  • 软件测试基础知识 - 集成测试和系统测试的区别,以及它们的应用场景
    区别1、测试计划和测试用例编制的先后顺序:从V模型来讲,在需求阶段就要制定系统测试计划和测试用例,概要设计的时候做集成测试计划和测试用例,有些公司的具体实践不一样,但是顺序肯定是先做系统测试计划和测试用例,再做集成测试计划和测试用例。2、测试用例的粒度:系统测试用例相对很接......