A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/forward_list/forward_list/ below:

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 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<iterator>::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)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4