forward_list

forward_list是C++11版本才有的。forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些。forward_list设计的时候就是追求效率的,跟我们自己写的C格式的单链表一样的高效。

考虑到效率问题,forward_list没有size成员函数。由于它本质是一个链表,有一个size成员会耗费常量的时间来计数其大小。这将需要一些额外的空间而且会降低插入和删除操作的效率。如果要获得forward_list 的大小,可以对begin和end调用distance算法操作。

forward_list 的大部分操作还是跟list差不多的,这里就简单看一些不一样的操作:

迭代器:

List是双向链表,所以有反向迭代器;而forward_list就没有反向迭代器。但是比list多了两种迭代器:

iterator before_begin() noexcept;

const_iterator before_begin() const noexcept;

 

const_iterator cbefore_begin() const noexcept;

这两种迭代器指向第一个元素之前的位置,所以不能解引用。它可以用做emplace_afterinsert_aftererase_after or splice_after的参数。

添加删除元素的操作:

template <class... Args> void emplace_front (Args&&... args);

在链表头节点的前面插入一个节点,新节点是用args做参数构造出来的。

void push_front (const value_type& val);
void push_front (value_type&& val);
在链表头节点的前面插入一个节点,但是与emplace_front不同的地方就是新节点是通过拷贝或者转移了val的。
template <class... Args>   iterator emplace_after (const_iterator position, Args&&... args);
 
iterator insert_after ( const_iterator position, const value_type& val );
iterator insert_after ( const_iterator position, value_type&& val );
iterator insert_after ( const_iterator position, size_type n, const value_type& val );
template <class InputIterator>  iterator insert_after ( const_iterator position, InputIterator first, InputIterator last );
iterator insert_after ( const_iterator position, initializer_list<value_type> il );

在position之后插入新节点

iterator erase_after (const_iterator position);  
iterator erase_after (const_iterator position, const_iterator last);

删除position位置的节点

void pop_front();

删除第一个节点

因为forward_list是单链表,为了能跟list有想通的功能,所以就需要before_begin迭代器,使得可以在头节点之前插入新节点。

其他的函数都跟list的差不多,这里就不重复了。需要记住的就是forward_list没有size成员函数。需要自己计算获取其大小。

 

QLinkedList

QLinkedList才是Qt真正意义上的链表。QLinkedList实际上跟std::list是一样的,也是双向链表。QList实际内部还是一个指针数组。QLinkedList提供的函数大部分还是跟QList一样的,下面我们就看一些不同的地方。

int QLinkedList::size () const

返回链表的大小,即节点个数。而forward_list没有这个函数。

const_iterator QList::constBegin () const

const_iterator QList::constEnd () const

这是QList返回STL风格的迭代器。QLinkedList则没有提供这样的函数。

void QList::append ( const T & value )

void QList::append ( const QList<T> & value )

void QLinkedList::append ( const T & value )

QLinkedList 没有接受一个QLinkedList 参数的append函数。

最重要的区别就是:

QLinkedList没有 T &

operator[] ( int i )

链表是肯定不能支持索引操作的。

所以QList提供的所有基于索引的操作,QLinkedList都是不可能支持的,比如:

const T &

at ( int i ) const

void

insert ( int i, const T & value )

void

move ( int from, int to )

void

replace ( int i, const T & value )

T

takeAt ( int i )

void

swap ( int i, int j )

可能还有其他函数,这里就不一一列举了。

QLinkedList提供了与std::list的转换函数:

QLinkedList<T>

fromStdList ( const std::list<T> & list )

std::list<T>QLinkedList::toStdList () const

需要记住的一点:Qt容器是隐式共享的。QLinkedList自然也是支持隐式共享的。

QLinkedList和std::forward_list的更多相关文章

  1. QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)

    forward_list forward_list是C++11版本才有的.forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些.forwa ...

  2. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  3. C++ std::forward_list 基本用法

    #include <iostream> #include <string> #include <forward_list> using namespace std; ...

  4. C++ std::list 和 std::forward_list 的差别及其成员函数差异对比

    主要差别: list 是双向链表,forward_list 是双向链表. 成员函数差异: 函数名 list forward_list back() has no size() has no inser ...

  5. std::forward_list

    forward_list相比list来说空间利用率更好,与list一样不支持随机访问,若要访问除头尾节点的其他节点则时间复杂度为线性. 在forward_list成员函数里只能访问头节点以及向头节点插 ...

  6. forward_list详解

    forward_list在头文件<forward_list>中,与list类似,区别就是list时双链表,forward_list是单链表,forward_list只支持前向迭代.在访问第 ...

  7. C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...

  8. C++ std::list 基本用法

    #include <iostream> #include <string> #include <list> using namespace std; // http ...

  9. std::map使用结构体自定义键值

    使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...

随机推荐

  1. WCF编写时候的测试

    1右击WCF创建到使用到发布这篇文章中的类库项目中的接口类实现文件添加断点 2右击WCF创建到使用到发布这篇文章中的WCF服务网站设为启动项并允许 3右击WCF创建到使用到发布这篇文章中的WPF项目调 ...

  2. openwrt看IP流量

    可以利用iptable来实现的,看附件的脚本. 把他放到路由器里面,然后运行. #!/bin/sh echo "Collecting data..." echo "&qu ...

  3. SSH框架的简化

    ---恢复内容开始--- 一.简易化的SSH框架,如图: SSH框架的搭建,我就不多说了. 二.简易的ssh框架的步骤: 1.重新编写applicationContext.xmlwen文件 <一 ...

  4. HDU2955-Robberies

    描述: The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usual ...

  5. 第一个Hadoop程序——Hello Hadoop

    本人原创,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/12773163 下载Hadoop程序包,下载地址:http://hadoop ...

  6. CSS3 Media Query

    在移动端火爆的今日,一个好的web应用不仅仅要有对应移动平台的APP,自己web网页也需要对不同屏幕大小的移动设备进行支持,也就是我们所说的响应式web页面. 本篇文章就来介绍下最常见的响应式页面的实 ...

  7. hdu 3032 Nim or not Nim? 打sg表

    题目链接 给出n堆石子, 每次可以取一堆中的任意x个(x>=1), 或者将一堆石子拆成两堆, 取到最后一堆的胜. 这个题需要打sg表找规律, 打表程序看代码. #include<bits/ ...

  8. Django一对多,多对多操作

    简要说明 Django里面的数据库操作O2O&M2M,一般归属于models管理 使用场景 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了).//两个表的 ...

  9. 了解常见的浏览器内核 Trident,Geckos,Presto,Webkit

    了解常见的浏览器内核 Trident,Geckos,Presto,Webkit 内核只是一个通俗的说法,英文名称为"Layout engine",翻译过来就是"排版引擎& ...

  10. 去掉Flex4生成的SWF加载时的进度条

    原文 http://www.cnblogs.com/modou/articles/1955507.html 方法一: <?xml version="1.0" encoding ...