QLinkedList和std::forward_list
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自然也是支持隐式共享的。
QLinkedList和std::forward_list的更多相关文章
- QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)
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 ...
- C++ std::forward_list 基本用法
#include <iostream> #include <string> #include <forward_list> using namespace std; ...
- C++ std::list 和 std::forward_list 的差别及其成员函数差异对比
主要差别: list 是双向链表,forward_list 是双向链表. 成员函数差异: 函数名 list forward_list back() has no size() has no inser ...
- std::forward_list
forward_list相比list来说空间利用率更好,与list一样不支持随机访问,若要访问除头尾节点的其他节点则时间复杂度为线性. 在forward_list成员函数里只能访问头节点以及向头节点插 ...
- forward_list详解
forward_list在头文件<forward_list>中,与list类似,区别就是list时双链表,forward_list是单链表,forward_list只支持前向迭代.在访问第 ...
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...
- C++ std::list 基本用法
#include <iostream> #include <string> #include <list> using namespace std; // http ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
随机推荐
- 浅谈Spring(三)
一.基础Spring的标准测试 1.导入spring与junit继承的jar 2.引入注解 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfig ...
- win7 ie10输入网址显示无法显示此页问题的解决
忽然又一天,非常奇怪,所有的浏览器都无法访问网页,直接输入IP也是不行. 本人试过各种方法,包括用360进行修复:清除%temp%下文件:看是否设错了dns, 升级和修复IE,重新注册ie相关的dll ...
- ADODB 数据库Access连接
<?php $filepath=__FILE__;//echo __FILE__;$newarray=explode("\\",$filepath);$num=count($ ...
- Linux系统环境变量的四个配置文件的关系
Linux系统环境变量配置有四个文件分别是: /etc/environment,/etc/profile,/etc/bash.bashrc,~/.bashrc 各配置文件意义 /etc/environ ...
- hdu 3529 Bomberman - Just Search! 重复覆盖
题目链接 依然是重复覆盖的模板.... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #d ...
- python strip()函数介绍
函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s) 删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s) 删除str ...
- C语言迭代求解
date : 2013/8/12 desinger :pengxiaoen 今天看 国外电子信息科学经典教材系列 <电子电路分析与设计> 电子工业出版社的 的19 ...
- XSS CSRF
XSS CSRF XSS 参考 https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC ...
- Yii2简单纪要
网上经常拿Yii来类比ROR,从MVC角度,使用体验及代码风格上确实有很多相似的地方.不过看配置文件发现Yii2不止是受rails的影响,同样有不少spring的影子,最明显的就是配置文件中很多IOC ...
- iOS开发宝典:String用法大全
一.NSString 创建字符串. NSString *astring = @"This is a String!"; 创建空字符串,给予赋值. NSString *astri ...