主要差别:

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. JS页面跳转和打开新窗口方式

    1.window.location.href=URL : 在本窗体打开一个新的页面,也是最常用的一种方法: 2.window.open(URL)  :  在一个新的窗口打开一个新的页面: 3.loca ...

  2. 改进一条Group By

    1.先回顾下基础知识: Group By 对数据分组聚合,常常伴随having使用.having可以处理单记录,也可以小组为单位处理. 语句: SELECT colFROM table[WHERE][ ...

  3. python爬虫--模拟12306登录

    模拟12306登录 超级鹰: #!/usr/bin/env python # coding:utf-8 import requests from hashlib import md5 class Ch ...

  4. SQL中Group By和having的用法

    转自 ITGirl笑笑   一.GROUP BY GROUP BY语句用来与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX. ...

  5. ClassNotFoundException------IDEA下的一种原因

    由于直接复制文件而未经过IDE造成次异常,需要修改程序入口:

  6. ELK查询命令详解

    目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...

  7. npm 镜像配置

    npm 默认 registry:https://registry.npmjs.org/  npm中文文档:https://www.npmjs.cn   淘宝 NPM 镜像:https://npm.ta ...

  8. Hystrix集群及集群监控turbine

    Hystrix集群及监控turbine 前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine. turbine是基于Dashboard的. 先搞个 ...

  9. Newifi-mini OpenWrt 下 EAP-PEAP,EAP-TLS 企业级无线认证及 FreeRadius3

    Newifi-mini OpenWrt 下 EAP-PEAP,EAP-TLS 企业级无线认证及 FreeRadius3 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-07-15. ...

  10. Openshift 自建DDNS动态域名

    某种情况下如openstack或者openshift/kubernetes软件部署过程由于需要标准的 域名系统(DNS UPDATE)RFC 2136中的动态更新功能, 但是现有的阿里云/华为云均不提 ...