This file contains implementation of ForwardList class.
More...
#include <cstddef>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <limits>
#include <memory>
#include <ranges>
#include <utility>
Go to the source code of this file.
This file contains implementation of ForwardList class.
- Author
- Michal Zygmunt
- Copyright
- Copyright (c) 2025 Michal Zygmunt This project is distributed under the MIT License. See accompanying LICENSE.txt file or obtain copy at https://opensource.org/license/mit
Definition in file forward_list.h.
◆ erase()
template<typename T, typename U>
Function erases from container all elements that are equal to value.
- Template Parameters
-
| T | data type stored in containers |
| U | data type of value |
- Parameters
-
| [in,out] | container | object to remove erase elements from |
| [in] | value | value to remove from container |
- Returns
- size_type number of elements removed
Definition at line 1974 of file forward_list.h.
1975 {
1976 return erase_if(container, [&value](U node_val) {
return node_val == value; });
1977 }
auto erase_if(ForwardList< T > &container, Pred pred) -> ForwardList< T >::size_type
Function erases from container all elements that satisfy the predicate pred.
◆ erase_if()
template<typename T, typename Pred>
Function erases from container all elements that satisfy the predicate pred.
- Template Parameters
-
| T | data type stored in containers |
| Pred | predicate to check if element should be erased |
- Parameters
-
| [in,out] | container | container object to remove erase elements from |
| [in] | pred | predicate which returns true if the element should be erased |
- Returns
- size_type number of elements removed
Definition at line 1989 of file forward_list.h.
1990 {
1992 }
auto remove_if(UnaryPred predicate) -> size_type
Function removes all elements for which predicate returns true.
◆ operator<<()
template<typename T>
| auto dsa::operator<< |
( |
std::ostream & | out, |
|
|
const ForwardList< T > & | list ) -> std::ostream&
|
Overloads operator to print all elements of ForwardList.
- Template Parameters
-
| T | type of initializer list elements |
- Parameters
-
| [in,out] | out | reference to output stream |
| [in] | list | ForwardList to print |
- Returns
- std::ostream&
Definition at line 1863 of file forward_list.h.
1864 {
1866 {
1867 return out;
1868 }
1869
1870 for (
auto it = list.
cbegin(); it != list.
cend(); ++it)
1871 {
1872 T value = *it;
1873 out << value << ' ';
1874 }
1875
1876 return out;
1877 }
auto cend() const noexcept -> const_iterator
Function returns pointer to ForwardList last Node.
auto cbegin() const noexcept -> const_iterator
Function returns const pointer to ForwardList first Node.
auto empty() const -> bool
Function checks if container has no elements.
◆ operator<=>()
template<typename T>
| auto dsa::operator<=> |
( |
const ForwardList< T > & | lhs, |
|
|
const ForwardList< T > & | rhs ) -> std::compare_three_way_result_t<T>
|
|
nodiscardnoexcept |
The relational operator compares two ForwardList objects.
Depending on type T, function returns one of following objects: std::strong_ordering::less / equal / greater std::weak_ordering::less / equivalent / greater std::partial_ordering::less / equivalent / greater / unordered It is best to compare results with 0 to determine if lhs is <, >, or == to rhs
- Parameters
-
| [in] | lhs | input container |
| [in] | rhs | input container |
- Returns
- three way comparison result type
Definition at line 1928 of file forward_list.h.
1930 {
1931 auto lhs_iter = lhs.
cbegin();
1932 auto rhs_iter = rhs.
cbegin();
1933
1934 while (lhs_iter != lhs.
cend() && rhs_iter != rhs.
cend())
1935 {
1936 auto cmp = *lhs_iter <=> *rhs_iter;
1937 if (cmp != 0)
1938 {
1939 return cmp;
1940 }
1941
1942 lhs_iter++;
1943 rhs_iter++;
1944 }
1945
1946
1947
1949 }
auto size() const -> size_type
Function returns ForwardList size.
◆ operator==()
The relational operator compares two ForwardList objects.
- Template Parameters
-
| T | type of data stored in ForwardList |
- Parameters
-
| [in] | lhs | input container |
| [in] | rhs | input container |
- Return values
-
| true | if containers are equal |
| false | if containers are not equal |
Definition at line 1889 of file forward_list.h.
1891 {
1893 {
1894 return false;
1895 }
1896
1897 auto lhs_iter = lhs.
cbegin();
1898 auto rhs_iter = rhs.
cbegin();
1899
1900 while (lhs_iter != lhs.
cend())
1901 {
1902 if (*lhs_iter != *rhs_iter)
1903 {
1904 return false;
1905 }
1906
1907 lhs_iter++;
1908 rhs_iter++;
1909 }
1910
1911 return true;
1912 }
◆ swap()
Exchanges content of two ForwardList containers.
- Template Parameters
-
| T | data type stored in containers |
- Parameters
-
| [in] | lhs | container to swap content |
| [in] | rhs | container to swap content |
Definition at line 1959 of file forward_list.h.
1960 {
1962 }
void swap(ForwardList< T > &other) noexcept(std::is_nothrow_swappable_v< T >)
Function swaps content of two ForwardList objects.