std::forward_list

template < class T, class Alloc = allocator > class forward_list;

Forward list

Forward lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence.

Forward lists are implemented as singly-linked lists; Singly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the next element in the sequence.

The main design difference between a forward_list container and a list container is that the first keeps internally only a link to the next element, while the latter keeps two links per element: one pointing to the next element and one to the preceding one, allowing efficient iteration in both directions, but consuming additional(额外的消耗) storage per element and with a slight higher time overhead inserting and removing elements. forward_list objects are thus more efficient than list objects, although they can only be iterated forwards.

Compared to other base standard sequence containers (array, vector and deque), forward_list perform generally better in inserting, extracting(提取) and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of forward_lists and lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a forward_list one has to iterate from the beginning to that position, which takes linear time in the distance between these. They also consume(消耗) some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

The forward_list class template has been designed with efficiency in mind: By design, it is as efficient as a simple handwritten C-style singly-linked list, and in fact is the only standard container to deliberately(从容的) lack(缺乏) a size member function for efficiency considerations: due to its nature as a linked list, having a size member that takes constant time would require it to keep an internal counter for its size (as list does). This would consume some extra storage and make insertion and removal operations slightly(轻微的) less efficient. To obtain the size of a forward_list object, you can use the distance algorithm with its begin and end, which is an operation that takes linear time.

Container properties

  • Sequence: Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
  • Linked list: Each element keeps information on how to locate the next element, allowing constant time insert and erase operations after a specific element (even of entire ranges), but no direct random access.
  • Allocator-aware: The container uses an allocator object to dynamically handle its storage needs.

Template parameters

  • T: Type of the elements. Aliased as member type forward_list::value_type.
  • Alloc: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent. Aliased as member type forward_list::allocator_type.

Member types

member type definition notes
value_type The first template parameter (T)
allocator_type The second template parameter (Alloc) defaults to: allocator<value_type>
reference value_type&
const_reference const value_type&
pointer allocator_traits<allocator_type>::pointer for the default allocator: value_type*
const_pointer allocator_traits<allocator_type>::const_pointer for the default allocator: const value_type*
iterator a forward iterator to value_type convertible to const_iterator
const_iterator a forward iterator to const value_type
difference_type a signed integral type, identical to: iterator_traits::difference_type usually the same as ptrdiff_t
size_type an unsigned integral type that can represent any non-negative value of difference_type usually the same as size_t

Member functions

  • (constructor): Construct forward_list object (public member function )
  • (destructor): Destroy forward_list object (public member function )
  • operator=: Assign content (public member function )

Iterators

  • before_begin: Return iterator to before beginning (public member function )
  • begin: Return iterator to beginning (public member type )
  • end: Return iterator to end (public member function )
  • cbefore_begin: Return const_iterator to before beginning (public member function )
  • cbegin: Return const_iterator to beginning (public member function )
  • cend: Return const_iterator to end (public member function )

Capacity(容量)

  • empty: Test whether array is empty (public member function )
  • max_size: Return maximum size (public member function )

Element access

  • front: Access first element (public member function )

Modifiers

  • assign: Assign content (public member function )
  • emplace_front: Construct and insert element at beginning (public member function )
  • push_front: Insert element at beginning (public member function )
  • pop_front: Delete first element (public member function )
  • emplace_after: Construct and insert element (public member function )
  • insert_after: Insert elements (public member function )
  • erase_after: Erase elements (public member function )
  • swap: Swap content (public member function )
  • resize: Change size (public member function )
  • clear: Clear content (public member function )

Operations

  • splice_after Transfer elements from another forward_list (public member function )
  • remove: Remove elements with specific value (public member function )
  • remove_if: Remove elements fulfilling condition (public member function template )
  • unique: Remove duplicate values (public member function )
  • merge: Merge sorted lists (public member function )
  • sort: Sort elements in container (public member function )
  • reverse: Reverse the order of elements (public member function )

Observers

  • get_allocator: Get allocator (public member function )

Non-member function overloads

  • relational operators (forward_list): Relational operators for forward_list (function template )
  • swap (forward_list): Exchanges the contents of two forward_list containers (function template )

Code Example

#include <iostream>
#include <forward_list>
#include <cmath> using namespace std; template<class Container>
Container by_two(const Container& x)
{
Container temp(x);
for(auto &x:temp)
x *= 2;
return temp;
} bool single_digit(const int& value){ return (value < 10); } class is_odd_class
{
public:
bool operator() (const int& value) {return (value%2)==1; }
} is_Odd_Object; bool same_integral_part(double first, double second)
{ return ( int(first) == int(second) ); } class is_near_class
{
public:
bool operator() (double first, double second)
{ return ( fabs( first - second )< 0.5 ); }
} is_near_Object; int main(int argc, char **argv)
{
forward_list<int> flist1;
forward_list<int> flist2(3, 77);
forward_list<int> flist3(flist2.begin(),flist2.end());
forward_list<int> flist4(flist3);
/** move ctor. (fourth wasted) 把flist4给flist5, 然后清空flist4 */
forward_list<int> flist5(move(flist4));
forward_list<int> flist6 = {3,52,25,90}; cout << "\n flist1: "; for(int &x: flist1) cout << x << '\t';
cout << "\n flist2: "; for(int &x: flist2) cout << x << '\t';
cout << "\n flist3: "; for(int &x: flist3) cout << x << '\t';
cout << "\n flist4: "; for(int &x: flist4) cout << x << '\t';
cout << "\n flist5: "; for(int &x: flist5) cout << x << '\t';
cout << "\n flist6: "; for(int &x: flist6) cout << x << '\t'; forward_list<int> second1(4);
forward_list<int> second2(3,5); second1 = second2;
second2 = by_two(second1);
cout << "\n second1:"; for(int &x:second1) cout << x;
cout << "\n second2:"; for(int &x:second2) cout << x; forward_list<int> third = {20,30,40,50};
third.insert_after( third.before_begin(), 11 );
cout << "\n third:"; for(int &x:third) cout << x; forward_list<int> four1 = {1,2,3};
forward_list<int> four2 = {10,20,30}; auto it = four1.begin();
four1.splice_after(four1.begin(), four2 );
/**
* first: 10 20 30 1 2 3
* four2: (empty)
* "it" still points to the 1 (now four1's 4th element)
* */ four2.splice_after( four2.begin(), four1, four1.begin(), it );
/** four1:10 1 2 3 four2: 20 30 */ four1.splice_after( four1.before_begin(), four2, four2.begin() );
/**
* four1:30 10 1 2 3 four2: 20
* notice that what is moved after the iterator
* */ four1.remove(10);
/** four1: 30 1 2 3 */ forward_list<int> five = {7,80,7,15,85,52,6};
five.remove_if(single_digit); ///< 80 15 85 52
five.remove_if(is_Odd_Object);///< 80 52 forward_list<double> six = {15.2, 73.0, 3.14, 15.85, 69.5, 73.0, 3.99,
15.2, 69.2, 18.5 }; six.sort(); ///< 3.14 3.99 15.2 15.2 15.85 18.5 69.2 69.5 73.0 73.0
six.unique(); ///< 3.14 3.99 15.2 15.85 18.5 69.2 69.5 73.0
six.unique( same_integral_part );///< 3.14 15.2 18.5 69.2 73.0
six.unique( is_near_Object ); ///< 3.14 15.2 69.2 forward_list<double> seven1 = {4.2, 2.9, 3.1};
forward_list<double> seven2 = {1.4, 7.7, 3.1};
forward_list<double> seven3 = {6.2, 3.7, 7.1}; seven1.sort(); ///< 2.9 3.1 4.2
seven2.sort(); ///< 1.4 3.1 7.7
seven1.merge(seven2); ///< 1.4 2.9 3.1 4.2 7.7 seven1.sort( greater<double>() ); ///< 7.7 4.2 3.1 2.9 1.4
seven3.sort( greater<double>() ); ///< 7.1 6.2 3.7
seven1.merge( seven3, greater<double>() );
///< 7.7 7.1 6.2 4.2 3.7 3.1 2.9 1.4 seven1.reverse();
/** 1.4 2.9 3.1 3.7 4.2 6.2 7.1 .7.7 */
return 0;
}

Reference

cplusplus


C++ std::forward_list的更多相关文章

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

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

  2. QLinkedList和std::forward_list

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

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

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

  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. ASP.NET Core 之 Identity 入门(三)

    前言 在上一篇文章中,我们学习了 CookieAuthentication 中间件,本篇的话主要看一下 Identity 本身. 最早2005年 ASP.NET 2.0 的时候开始, Web 应用程序 ...

  2. win7安装时,避免产生100m系统保留分区的办法

    在通过光盘或者U盘安装Win7操作系统时,在对新硬盘进行分区时,会自动产生100m的系统保留分区.对于有洁癖的人来说,这个不可见又删不掉的分区是个苦恼.下面介绍通过diskpart消灭保留分区的办法: ...

  3. [译] C# 5.0 中的 Async 和 Await (整理中...)

    C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...

  4. BPM Domino集成解决方案

    一.需求分析 Lotus Notes/Domino是IBM的协同办公平台,在国内有广泛的用户. 但由于推出年头较早.采用文档数据库等特点, 导致其流程集成能力弱.统计分析难.不支持移动办公等问题,很多 ...

  5. TabLayout + ViewPager

    一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...

  6. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  7. Mysql - 查询之关联查询

    查询这块是重中之重, 关系到系统反应时间. 项目做到后期, 都是要做性能测试和性能优化的, 优化的时候, 数据库这块是一个大头. sql格式: select 列名/* from 表名 where 条件 ...

  8. Linux虚拟化学习笔记<一>

    关于虚拟化,原理的东西是非常复杂的,要想完全理解,没有足够的耐心是不不能完全学透这部分内容的.那下面我主要以资源汇总的形式把一些资料罗列出来,帮助大家快速理解虚拟化,快速使用和配置. 为什么要虚拟化: ...

  9. Spring代理模式及AOP基本术语

    一.代理模式: 静态代理.动态代理 动态代理和静态代理区别?? 解析:静态代理需要手工编写代理类,代理类引用被代理对象. 动态代理是在内存中构建的,不需要手动编写代理类 代理的目的:是为了在原有的方法 ...

  10. Linux服务器安全配置

    众所周知,网络安全是一个非常重要的课题,而服务器是网络安全中最关键的环节.Linux被认为是一个比较安全的Internet服务器,作为一种开放源代码操作系统,一旦Linux系统中发现有安全漏洞,Int ...