主要差别:

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 的差别及其成员函数差异对比的更多相关文章

  1. c++11特性与cocos2d-x 3.0之std::bind与std::function

    昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...

  2. c++ 如何获取多线程的返回值?(std::thread ,std::async)

    //简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...

  3. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  4. 使用std::map和std::list存放数据,消耗内存比实际数据大得多

    使用std::map和std::list存放数据,消耗内存比实际数据大得多 场景:项目中需要存储一个结构,如下程序段中TEST_DATA_STRU,结构占24B.但是使用代码中的std::list&l ...

  5. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  6. 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 ...

  7. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  8. 解决程序出现“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 ...

  9. 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...

随机推荐

  1. 基于iCamera测试光电大赛官方指定摄像头mt9m001调试小结

    基于iCamera测试光电大赛官方指定摄像头mt9m001调试小结 先看看官方的接口 组委会指定的模块接口 信号定义说明: VDD:3.3v GND:地 SCK:摄像头寄存器的iic配置信号的时钟线 ...

  2. 【搞定Jvm面试】 JVM 垃圾回收揭秘附常见面试题解析

    JVM 垃圾回收 写在前面 本节常见面试题 问题答案在文中都有提到 如何判断对象是否死亡(两种方法). 简单的介绍一下强引用.软引用.弱引用.虚引用(虚引用与软引用和弱引用的区别.使用软引用能带来的好 ...

  3. 105道BAT最新Java面试题(MySQL+Redis+nginx+ookeeper+MongoDB)

    MySQL面试题 1. 主键 超键 候选键 外键 2.数据库事务的四个特性及含义 3. 视图的作用,视图可以更改么? 4. drop,delete与truncate的区别 5. 索引的工作原理及其种类 ...

  4. Unity学习路线

    转载请标明原文地址:http://www.cnblogs.com/zhangyukof/p/7068615.html 1.Unity下载安装和破解方法: http://blog.sina.com.cn ...

  5. 初步了解JVM第三篇(堆和GC回收算法)

    在<初步了解JVM第一篇>和<初步了解JVM第二篇>中,分别介绍了: 类加载器:负责加载*.class文件,将字节码内容加载到内存中.其中类加载器的类型有如下:执行引擎:负责解 ...

  6. SSM整合框架(基于IDEA的配置)

    Pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...

  7. CouchDB学习-介绍

    官方文档 CouchDB 1文档存储 CouchDB服务器主机是一个存储文档的数据库.每一个文档在数据库中都有唯一的名字.CouchDB提供RESTful HTTP API用来读取和更新(添加,编辑, ...

  8. 一个proc预编译代码时coredump的问题分析

    最近有同事在搞编译环境迁移,碰上一个问题让我帮他看一下.    他建了一个新目录,然后把现在的代码拷过去,编译的时候发现有一个文件编译不了一执行就出现core,不知道啥情况.    我进到他的编译环境 ...

  9. Oracle Proc编程性能优化经验

    Proc 是Oracle提供的一种数据库操作的API.它是基于ESql技术的,需要预编译后才可以变成普通c代码,非常不直观,使用起来不太方便,阅读也存在困难. 因为这些问题导致程序员平时开发中会出现一 ...

  10. CentOS 7上的系统管理之:Systemd和systemctl

    参考资料: Chapter 10. Managing Services with systemd Red Hat Enterprise Linux 7 | Red Hat Customer Porta ...