QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)
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_after, insert_after, erase_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自然也是支持隐式共享的。
http://blog.csdn.net/hai200501019/article/details/11787097
QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)的更多相关文章
- QLinkedList和std::forward_list
forward_list forward_list是C++11版本才有的.forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些.forwa ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- std::forward_list
forward_list相比list来说空间利用率更好,与list一样不支持随机访问,若要访问除头尾节点的其他节点则时间复杂度为线性. 在forward_list成员函数里只能访问头节点以及向头节点插 ...
- C++ std::forward_list 基本用法
#include <iostream> #include <string> #include <forward_list> using namespace std; ...
- stdafx.h、stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编
sstdafx.h.stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要 ...
- C++ std::list 和 std::forward_list 的差别及其成员函数差异对比
主要差别: list 是双向链表,forward_list 是双向链表. 成员函数差异: 函数名 list forward_list back() has no size() has no inser ...
- 为什么都反对XML而支持使用json呢?
一个使用上的因素:JSON的结构更容易映射至一般语言的数据结构. XML和JSON的主要组成成分: XML是element.attribute和element content. JSON是object ...
- c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法
在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误. 这是为什么呢?原因在于什么?如何解决? 看下面一个例子: int main(int, char*[]) { st ...
- react使用map生成的元素,key的设定不对导致每次删除都删除最后一个
假设 你的key设置为map中的索引,假设为0,1,2(原dom树),现在你用splice删除掉1,重新渲染时,还是会按map索引按顺序渲染为0,1(新dom树),由于react渲染机制是比较的key ...
随机推荐
- Oracle游标循环更新数据案例
declare v_XTXMBH number; v_ZJZJZJRQ varchar2(40); cursor c_job is SELECT XT.XTXMBH AS XTXMBH, QJ.ZJZ ...
- [翻译]Django速查表
原文在此:https://code.djangoproject.com/wiki/DjangoCheatSheet Django速查表Django教程已经非常好了.这个速查表的作用是创建一个快速开始指 ...
- 谓词--Predicate
去苹果的的技术官网搜索-Predicate就会找到相关的文档-Predicate Programming Guide 1,创建谓词时 %@是变量时不加单双引号,常量是加单引号,加双引号需要转义符号\ ...
- VPN各种方案
http://www.maimiaovpn.com 大家都是用什么方法FQ的?我原来用的XskyWalker浏览器 但我用的电信网现在不行了.大家用的什么方法可否告诉我一下我用yes-vpn,10元一 ...
- 部署一个class文件
只发布一个class文件找到项目工作空间/target/class..根据项目结构找到修改的java文件编译的class文件比如RegexUtils.class使用SecureFXPortable将文 ...
- hibernate的配置 1
hibernate 是一种ORM框架,是ORM框架中一个典范 ORM叫做对象关系映射 是面向对象语言和关系型数据库之间的映射关系 所以只有在面向对象语言或者关系型数据库没用的时候ORM才会消失 ORM ...
- 开发记录_自学Python写爬虫程序爬取csdn个人博客信息
每天刷开csdn的博客,看到一整个页面,其实对我而言,我只想看看访问量有没有上涨而已... 于是萌生了一个想法: 想写一个爬虫程序把csdn博客上边的访问量和评论数都爬下来. 打算通过网络各种搜集资料 ...
- C# RSA在服务上使用出现拒绝方法错误的解决方法
在做一个快钱接口的时候,遇到了.net RSA加密无法在一台win2008服务器上运行正常,更换到Win2003服务器后出现问题,具体表现如下: “/”应用程序中的服务器错误. ----------- ...
- hdu1533解题报告
题意:这里有一个N*M的方格图.....图中m代表人,H代表房子...并且人数和房子的数量是相等的..那么.每个人可以竖直或者横向走一格,并且花费1S元...那么为了让所有的人进入房子,求解最小的花费 ...
- 你会用swift创建复杂的加载动画吗(1)
时至今日,iOS 应用商店已经拥有超过了140万 应用,让你自己的应用脱颖而出确实是个不小的挑战.不过,在你的应用掉入默默无闻的大黑洞之前,你拥有一个小小的机遇窗,它能帮你吸引用户的注意. AD: 时 ...