list
overcomes some limitations of vector
std::list
API¶Defined in header <list>
namespace std {
template <
typename T, // value type to store
typename Allocator = std::allocator<T> // how to allocate data
> class list;
}
std::list
Special Member Datatypes¶Name | Description |
---|---|
T |
template parameter for value type to store |
value_type |
alias to T |
size_type |
unsigned integer type (usually std::size_t ) |
difference_type |
signed integer type (usually std::ptrdiff_t ) |
reference |
alias to value_type& |
const_reference |
alias to value_type const& |
pointer |
usually just value_type* |
const_pointer |
usually just value_type const* |
iterator |
implementation-defined type |
const_iterator |
implementation-defined type |
reverse_iterator |
std::reverse_iterator<iterator> |
const_reverse_iterator |
std::reverse_iterator<const_iterator> |
std::list
Contructors¶list ();
list (size_type n);
list (size_type n, T const& value);
template <typename InputIterator>
list (InputIterator first, InputIterator last);
list (std::initializer_list<T>);
list()
std::list<int>()
{}
list(size_type n)
std::list<int>(5)
{ 0, 0, 0, 0, 0 }
list(size_type n, T const& value)
std::list<int>(5, 2)
{ 2, 2, 2, 2, 2 }
template <typename InputIterator>
list(InputIterator first, InputIterator last);
[first, last)
list(std::initializer_list<T> list);
std::list<int> l = {5, 4, 3, 2, 1};
l
{ 5, 4, 3, 2, 1 }
std::list
Access Operations¶const_reference back () const;
const_reference front () const;
reference back ();
reference front ();
reference back ();
const_reference back() const;
const
version of back()
[] () -> int& {
static std::list<int> a = {1, 2, 3};
return a.back();
}()
3
[] () -> int const& {
const static std::list<int> a = {1, 2, 3};
return a.back();
}()
3
reference front ();
const_reference front() const;
const
version of front()
[] () -> int& {
static std::list<int> a = {1, 2, 3};
return a.front();
}()
1
[] () -> int const& {
const static std::list<int> a = {1, 2, 3};
return a.front();
}()
1
std::list
Capacity Operations¶bool empty () const;
size_type size () const;
size_type max_size () const;
bool empty() const;
true
IFF the vector's size is zero
size_type size () const
size_type max_size() const;
std::list
Iterator Operations¶iterator begin () noexcept;
iterator end () noexcept;
reverse_iterator rbegin () noexcept;
reverse_iterator rend () noexcept;
const_iterator begin () const noexcept; // and cbegin()
const_iterator end () const noexcept; // and cend()
const_reverse_iterator rbegin () const noexcept; // and crbegin()
const_reverse_iterator rend () const noexcept; // and crend()
std::list
Modifying Operations¶Similar to std::vector
!
void clear();
void pop_back ();
iterator erase (...); // 2 versions
iterator insert (...); // 5 versions
void push_back (const_reference value); // and "move" variant
template <typename ... Args>
iterator emplace (const_iterator pos, Args&& ... value);
template <typename ... Args>
reference emplace_back (Args&& ... value);
std::list
Modifying Operations¶void pop_front ();
void push_front (const_reference value); // and "move" variant
template <typename ... Args>
reference emplace_front (Args&& ... value);
void resize(...); // 2 versions
void push_back (const_reference value);
void pop_back ();
void push_front (const_reference value);
void pop_front ();
iterator insert (const_iterator pos, const_reference value);
iterator erase (const_iterator pos);
iterator erase (const_iterator first, const_iterator last);
[first, last)
[first, last)
are invalidatedstd::distance(first, last)
; the same number of elements are removed
void resize(size_type n);
T()
is filled for the increase in elements at the end. If the size is decreased, the original values at the front are retained.Transfer elements from one list to another in constant time
void splice (const_iterator pos, list& other);
void splice (const_iterator pos, list&& other);
void splice (const_iterator pos, list& other, const_iterator it);
void splice (const_iterator pos, list&& other, const_iterator it);
void splice (const_iterator pos, list& other, const_iterator first, const_iterator last);
void splice (const_iterator pos, list&& other, const_iterator first, const_iterator last);
*this
at location $pos$, causing $other$ to be empty*this
at location $pos$*this
at location $pos$Removes all elements satisfying specific criteria. The first version removes all elements that are equal to $value$, the second version removes all elements for which predicate $p$ returns true
.
size_type remove (T const& value);
template <typename UnaryPredicate>
size_type remove_if (UnaryPredicate p);
void reverse ();
size_type unique();
template <typename BinaryPredicate>
size_type unique (BinaryPredicate p);
void sort();
template <typename Compare>
void sort (Compare comp);
reverse
-- reverses the order of the elementsunique
-- removes consecutive duplicate elementssort
-- Sorts the elements in ascending order. Stable. $O(N lg N)$list<int>::iterator li;
#include <iterator>
std::advance (li, 2); // li += 2
li2 = std::next (li, 2); // li2 = li + 2;
li3 = std::prev (li, 3); // li2 = li - 3;
template <typename T>
class list {
public:
class iterator {
public:
T& operator*();
T const& operator*();
T* operator->();
T const* operator->();
iterator& operator++();
iterator operator++(int) const;
iterator& operator--();
iterator operator--(int) const;
bool operator==(iterator const& other) const;
bool operator!=(iterator const& other) const;
};
...
};
// Using std::list
Examples to follow...