主要差别:

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. Day 03 作业

    简述变量的组成 变量名,赋值符号,变量值 简述变量名的命名规范 变量名应该能反映变量值所描述的状态 变量名必须以字母数字下划线组合且不能以数字开头 变量名不能是关键字 简述注释的作用 让后面的代码失效 ...

  2. 【C语言】%f,%lf,%3.1f

    在输出时应注意变量类型,使用如%3.1时会默认四舍五入.

  3. 机会来了!5G时代带来新闻传播行业的变革!

    5G时代到来!新闻传播行业大变革! 1.作为一名体育生进入的新闻传播学院,传统的新闻媒体能力已不再具有优势,意味着我有翻身的机会了! 从一开始进入大学,由于高中的知识储备不如其他人,尤其是英语能力方面 ...

  4. final关键字、finally代码块和finalize()方法有什么区别?

    1. final是关键字,final可以修饰类.方法.属性. 如果一个类被final修饰,那么这个类就是最终类,不能派生出新的子类,不能作为父类被继承,该类中的所有方法都不能被重写,但是final类中 ...

  5. Centos7 Openresty 开发环境

    首先要安装编译环境 yum group install "Development Tools" yum install pcre-devel openssl-devel gcc c ...

  6. NTFS在openwrt下的挂载问题

    在openwrt上市可以挂载ntfs分区的,但是如果原来如果搞过win,或者异常关机,那么会遇到以下的错误: root@Openwrt:/etc/config# mount -t ntfs -o rw ...

  7. Node6-1单元测试mocha

    1.初步简单的测试 在src新建一个math.js的文件.src/math.js module.exports ={ add:(...args)=>{ return args.reduce((p ...

  8. 张亦总结《AG百家乐庄闲,龙虎中下三路技巧和三株路的运用》

    ​​关于三珠路的各种打法,这里我做个详细的讲解 三珠路的打法源于澳门赌王叶汉,需要了解赌王叶汉的著作<BJL投注公式>的可以加我Q:<10353581>进行详细解答 叶汉的打法 ...

  9. Homebrew的安装

    Homebrew是一款Mac OS平台下的软件包管理工具. 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserc ...

  10. Java并发之synchronized关键字深度解析(三)

    前言 本篇主要介绍一下synchronized的批量重偏向和批量撤销机制,属于深水区,大家提前备好氧气瓶. 上一篇说完synchronized锁的膨胀过程,下面我们再延伸一下synchronized锁 ...