C++ std::list 和 std::forward_list 的差别及其成员函数差异对比
主要差别:
list 是双向链表,forward_list 是双向链表。
成员函数差异:
| 函数名 | list | forward_list |
| back() | has | no |
| size() | has | no |
| insert() | has | no |
| emplace() | has | no |
| erase() | has | no |
| push_back() | has | no |
| emplace_back() | has | no |
| splice() | has | no |
| before_begin() | no | has |
| cbefore_begin() | no | has |
| insert_after() | no | has |
| emplace_after() | no | has |
| erase_after() | no | has |
| splice_after() | no | has |
* forward_list 中设计的一系列 xxx_after() 的原因:
其中的元素仅知道后面的元素,不知道前面的元素。(单向链表的特性)所以类似于 insert 这样的操作,需要指定前一个元素的迭代器,然后执行插入,才可以把整个链表连接起来。
https://www.cnblogs.com/wuchanming/p/3915567.html
测试代码:
#include <iostream>
#include <string>
#include <list>
#include <forward_list> using namespace std; int main()
{ list<int> lis5 = {1,2,3};
forward_list<int> flis5 = { 1,2,3 }; ////////////////////////////////////////////////////////////////////////// lis5.insert(lis5.begin(), 44); // 44 1 2 3, 相当于 “insert_before”,不过并没有这个函数。
flis5.insert_after(flis5.begin(), 44); // 1 44 2 3 }
C++ std::list 和 std::forward_list 的差别及其成员函数差异对比的更多相关文章
- c++11特性与cocos2d-x 3.0之std::bind与std::function
昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...
- c++ 如何获取多线程的返回值?(std::thread ,std::async)
//简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...
- C++ std::unordered_map使用std::string和char *作key对比
最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...
- 使用std::map和std::list存放数据,消耗内存比实际数据大得多
使用std::map和std::list存放数据,消耗内存比实际数据大得多 场景:项目中需要存储一个结构,如下程序段中TEST_DATA_STRU,结构占24B.但是使用代码中的std::list&l ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- std::unique_lock<std::mutex> or std::lock_guard<std::mutex> C++11 区别
http://stackoverflow.com/questions/20516773/stdunique-lockstdmutex-or-stdlock-guardstdmutex The diff ...
- C++11之std::function和std::bind
std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...
- 解决程序出现“terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)”的问题
最近跑程序时出现了这么一个问题: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_al ...
- 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...
随机推荐
- Python3 并发编程4
目录 Event事件 线程池与进程池 基本概念 使用方法 和信号量的区别 协程(coroutine) 基本概念 实现方式 多线程爬取梨视频 Event事件 用来控制线程的执行 e.isSet()查看对 ...
- 每周一练 之 数据结构与算法(Set)
这是第四周的练习题,五一放假结束,该收拾好状态啦. 下面是之前分享的链接: 1.每周一练 之 数据结构与算法(Stack) 2.每周一练 之 数据结构与算法(LinkedList) 2.每周一练 之 ...
- Cisco packet tracer 的手动添加模块
在PacketTracer 里面,路由器都是基本配置,这和真实设备是相同的 基本配置里面2620只有一个以太网口: 而2621和2811在背板上有两个以太网接口 所以,你在show run里面可以看到 ...
- 【Git】本地分支
[Git]本地分支 转载:https://www.cnblogs.com/yangchongxing/p/10221382.html 目录 ============================== ...
- verilog设计加法器
概述 本文利用了硬件行为描述.数据流描述.结构描述三种方法分别写了几个加法器 一位半加法器 即两个一位的二进制数相加,得到其正常相加的结果的最后一位. 仿真波形图 硬件行为描述 设计文件 123456 ...
- 真伪随机数 ——Random和SecureRandom
Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...
- Nginx 常用配置方式说明
原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/7619763f-1 ...
- 原创 Hive left join 技巧总结
根据工作中经验总结出来 left join 常用的 使用注意点: A Left join B on A.id = B.id 第一种情况: 如果 A 表 ...
- CouchDB学习-介绍
官方文档 CouchDB 1文档存储 CouchDB服务器主机是一个存储文档的数据库.每一个文档在数据库中都有唯一的名字.CouchDB提供RESTful HTTP API用来读取和更新(添加,编辑, ...
- C# 中的栈和堆
程序运行时,它的数据必须存储在内存中.一个数据项需要多大的内存.存储在内存中的什么位置.以及如何存储都依赖于该数据项的类型. 运行中的程序使用两个内存区域来存储数据:栈和堆. 栈 栈是一个内存数组,是 ...