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 函数进阶2
目录 迭代器 可迭代对象 迭代器对象 总结和补充 列表推导式 字典生成式 zip()函数 递归 迭代器 迭代器是访问容器类数据类型元素的一种方式 迭代器是一个可以记住遍历的位置的对象 迭代器对象从容器 ...
- 从项目中理解SSM框架
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- 安卓逆向基础(001)-APK安装流程
1.在/data/app下以报名为文件夹名新建文件夹 APK包存放在这里 以及lib文件 存放so 2./data/dalvik-cache 存放dex dex是dalvik虚拟机可执行文件 3./d ...
- VS2017 无法修改代码编辑区的项背景颜色问题
以前都是好好的,安装 ClaudiaIDE: https://github.com/buchizo/ClaudiaIDE 之后也没啥问题,用着用着代码编辑区自定义的颜色就没有了,好几台电脑都是这样, ...
- [ASP.NET Core 3框架揭秘] 依赖注入[6]:服务注册
通过<利用容器提供服务>我们知道作为依赖注入容器的IServiceProvider对象是通过调用IServiceCollection接口的扩展方法BuildServiceProvider创 ...
- 离线安装Redis 说明
安装Redis所需环境 需要Root权限 1. 准备压缩包解压 (这里我们准备安装到visible账户下的webdata文件夹下) // *****root账户***** cd /home/visib ...
- inux 网络监控分析
一.sar -n:查看网卡流量 -n 参数,他有6个不同的开关:DEV | EDEV | NFS | NFSD | SOCK | ALL .DEV显示网络接口信息,EDEV显示关于网络错误的统计数据, ...
- GetPrivateProfileString() 当 key 包含空格时,需要进行转义
使用 GetPrivateProfileString() 方法可以方便的读取 ini 格式文件中的内容,如: [section] tommy = worker 使用 C# 读取如下: 1. 先引入 G ...
- IoT缺德相关
https://github.com/nebgnahz/awesome-iot-hacks <揭秘家用路由器0day漏洞挖掘技术> Embedded Device Security: Pw ...
- Python 的 Geth 封装库 PyGeth
PyGeth 是一个 Python 封装库,用来作为子进程运行 geth. 系统依赖 该库需要 geth 可执行文件. 安装 pip install py-geth 快速启动 运行连接到 mainne ...