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. collectionView 中cell间距设置建议

    应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的. 使用-(CGFloat )collecti ...

  2. T4模板之基础篇

    一.回顾 上一篇文章 ——T4模板之菜菜鸟篇,我们囫囵吞枣的创建了与“T4模板”有关的文件.在创建各个文件的这一个过程中,我们对于T4模板有了那么丁点的认识.现在就带着之前的那些问题,正式的迈入对“T ...

  3. MQ学习(一)----JMS规范(转发整合)

    最近进行ActiveMQ的学习,总结下已被不时之需. JMS规范: JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的A ...

  4. hdu4099

    要想通这个题目应该很容易,由于斐波纳契数在近100项之后很大,早就超出long long了.而输入最长的序列才40个数字,所以大约保留前50位,前40位是没有误差的!!!其实,想想我们判断double ...

  5. 代码风格——Cocos2d-x学习历程(四)

    1.Cocos2d-x拥有一个包含其他所有头文件的文件"cocos2d.h".通常,我们只需要在使用时包含这个头文件,就可以使用引擎的全部功能了. 2.Cocos2d-x的类都放置 ...

  6. R与数据分析旧笔记(四)画地图练习

    > library(maps) > library(geosphere) 载入需要的程辑包:sp > map("state")#画美国地图 > map(&q ...

  7. 关于gradle /Users/xxxx/Documents/workspace/fontmanager/.gradle/2.2.1/taskArtifacts/cache.properties (No such file or directory)报错办法

    转自:http://www.cnblogs.com/raomengyang/p/4367620.html   Android Studio报错: What went wrong: java.io.Fi ...

  8. codeforces 632F. Magic Matrix

    题目链接 给一个n*n的矩阵, 问是否对角线上的元素全都为0, a[i][j]是否等于a[j][i], a[i][j]是否小于等于max(a[i][k], a[j][k]), k为任意值. 前两个都好 ...

  9. 一个失败的操作系统MULTICS

    Unix的诞生和Multics(Multiplexed Information and Computing System)是有一定渊源的.当时开发者Brian Kernighan开玩笑地戏称这个不完善 ...

  10. 百度统计和CNZZ到底有哪些不同?

    作为互联网门外汉,同时作为不专业的站长,我总是有很多问题想知道,比如百度统计和CNZZ在数据统计上总是有差异,就好像王婆卖瓜,自卖自夸,又好像公说公有理,婆说婆有理. 作为专业的不专业的市场人员,好奇 ...