GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::forward_list

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<forward_list>

  • C++11
    forward_list

forward_list

  • C++11
    forward_list::~forward_list
  • C++11
    forward_list::forward_list
  • member functions

    • C++11
      forward_list::assign
    • C++11
      forward_list::before_begin
    • C++11
      forward_list::begin
    • C++11
      forward_list::cbefore_begin
    • C++11
      forward_list::cbegin
    • C++11
      forward_list::cend
    • C++11
      forward_list::clear
    • C++11
      forward_list::emplace_after
    • C++11
      forward_list::emplace_front
    • C++11
      forward_list::empty
    • C++11
      forward_list::end
    • C++11
      forward_list::erase_after
    • C++11
      forward_list::front
    • C++11
      forward_list::get_allocator
    • C++11
      forward_list::insert_after
    • C++11
      forward_list::max_size
    • C++11
      forward_list::merge
    • C++11
      forward_list::operator=
    • C++11
      forward_list::pop_front
    • C++11
      forward_list::push_front
    • C++11
      forward_list::remove
    • C++11
      forward_list::remove_if
    • C++11
      forward_list::resize
    • C++11
      forward_list::reverse
    • C++11
      forward_list::sort
    • C++11
      forward_list::splice_after
    • C++11
      forward_list::swap
    • C++11
      forward_list::unique
  • non-member overloads

    • C++11
      relational operators (forward_list)
    • C++11
      swap (forward_list)
  • Reference
  • <forward_list>
  • forward_list
class template
<forward_list>

std::forward_list

template < class T, class Alloc = allocator<T> > 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 typedefinitionnotes
value_typeThe first template parameter (T)
allocator_typeThe second template parameter (Alloc)defaults to: allocator<value_type>
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<allocator_type>::pointerfor the default allocator: value_type*
const_pointerallocator_traits<allocator_type>::const_pointerfor the default allocator: const value_type*
iteratora forward iterator to value_typeconvertible to const_iterator
const_iteratora forward iterator to const value_type
difference_typea signed integral type, identical to: iterator_traits<iterator>::difference_typeusually the same as ptrdiff_t
size_typean unsigned integral type that can represent any non-negative value of difference_typeusually 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)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.