Namespaces
Variants
Views
Actions

C++ named requirements: ReversibleContainer

From cppreference.com
< cpp‎ | named req
 
 
C++ named requirements
Basic
Type properties
Library-Wide
Container
Container Elements
(C++11)

Iterator
Stream I/O
Formatters
(C++20)
Random Numbers
(C++11)    
Concurrency
(C++11)
(C++11)
Ranges
Other
(C++11)


 

A ReversibleContainer is a Container that has iterators that meet the requirements of either LegacyBidirectionalIterator or LegacyRandomAccessIterator. Such iterators allow a ReversibleContainer to be iterated over in reverse.

Contents

[edit] Requirements

A type satisfies ReversibleContainer if it satisfies Container, its iterator type belongs to the bidirectional or random access iterator categories and, given the following types and values, the semantic and complexity requirements in the tables below are satisfied:

Type Definition
X an ReversibleContainer type
T the value_type of X
Value Definition
a a value of type X

[edit] Types

Name Type Requirements
typename X::reverse_iterator std::reverse_iterator<X::iterator> an iterator type whose value type is T
typename X::const_reverse_iterator  std::reverse_iterator<X::const_iterator>  a constant iterator type whose value type is T

[edit] Expressions

The types reverse_iterator and const_reverse_iterator in the following table denote typename X::reverse_iterator and typename X::const_reverse_iterator respectively.

Expression Type Semantics  Complexity 
a.rbegin() reverse_iterator
const_reverse_iterator for constant a
reverse_iterator(a.end()) Constant
a.rend() reverse_iterator
const_reverse_iterator for constant a
reverse_iterator(a.begin()) Constant
a.crbegin() const_reverse_iterator const_cast<const X&>(a).rbegin() Constant
a.crend() const_reverse_iterator const_cast<const X&>(a).rend() Constant

[edit] Standard library

[edit] Example

The following example iterates over a vector (which has random-access iterators) in reverse.

#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v = {3, 1, 4, 1, 5, 9};
 
    for (std::vector<int>::const_reverse_iterator i{v.crbegin()}; i != v.crend(); ++i)
        std::cout << *i << ' ';
    std::cout << '\n';
}

Output:

9 5 1 4 1 3

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2105 C++98 typename X::const_reverse_iterator was
required to be an iterator type of value type const T
required to be a constant
iterator type of value type T