Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/ReversibleContainer"

From cppreference.com
< cpp‎ | named req
m (fmt, +newline)
m (Additional fmt; rm "Complexity" column for #Types table (it's compile time by definition for ∀ types))
Line 6: Line 6:
 
===Requirements===
 
===Requirements===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc|{{ttb|X}}|Container type}}
+
{{dsc|{{tt|X}}|Container type}}
{{dsc|{{ttb|T}}|Element type}}
+
{{dsc|{{tt|T}}|Element type}}
{{dsc|{{ttb|a}}|Objects of type {{ttb|X}}}}
+
{{dsc|{{c|a}}|Objects of type {{tt|X}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Types====
 
====Types====
 
{|class=wikitable
 
{|class=wikitable
!expression||return type||conditions||complexity
+
!Expression||Return type||Conditions
 
|-
 
|-
|{{c|X::reverse_iterator}}||iterator type whose value type is {{tt|T}}||{{c|reverse_iterator<iterator>}}||compile time
+
|{{c|X::reverse_iterator}}||Iterator type whose value type is {{tt|T}}||{{c|reverse_iterator<iterator>}}
 
|-
 
|-
|{{c|X::const_reverse_iterator}}||constant iterator type whose value type is {{tt|T}}||{{c|reverse_iterator<const_iterator>}}||compile time
+
|{{c|X::const_reverse_iterator}}||Constant iterator type whose value type is {{tt|T}}||{{c|reverse_iterator<const_iterator>}}
 
|-
 
|-
 
|}
 
|}
Line 23: Line 23:
 
====Member functions====
 
====Member functions====
 
{|class=wikitable
 
{|class=wikitable
!expression||return type||conditions||complexity
+
!Expression||Return type||Conditions||Complexity
 
|-
 
|-
|{{c|a.rbegin()}}||{{c|reverse_iterator}}; {{c|const_reverse_iterator}} for constant {{tt|a}}||{{c|reverse_iterator(end())}}||constant
+
|{{c|a.rbegin()}}||{{c|reverse_iterator}}; {{c|const_reverse_iterator}} for constant {{c|a}}||{{c|reverse_iterator(end())}}||Constant
 
|-
 
|-
|{{c|a.rend()}}||{{c|reverse_iterator}}; {{c|const_reverse_iterator}} for constant {{tt|a}}||{{c|reverse_iterator(begin())}}||constant
+
|{{c|a.rend()}}||{{c|reverse_iterator}}; {{c|const_reverse_iterator}} for constant {{c|a}}||{{c|reverse_iterator(begin())}}||Constant
 
|-
 
|-
|{{c|a.crbegin()}}||{{c|const_reverse_iterator}}||{{c|const_cast<X const&>(a).rbegin()}}||constant
+
|{{c|a.crbegin()}}||{{c|const_reverse_iterator}}||{{c|const_cast<X const&>(a).rbegin()}}||Constant
 
|-
 
|-
|{{c|a.crend()}}||{{c|const_reverse_iterator}}||{{c|const_cast<X const&>(a).rend()}}||constant
+
|{{c|a.crend()}}||{{c|const_reverse_iterator}}||{{c|const_cast<X const&>(a).rend()}}||Constant
 
|}
 
|}
  
Line 55: Line 55:
 
     std::vector<int> v = {3, 1, 4, 1, 5, 9};
 
     std::vector<int> v = {3, 1, 4, 1, 5, 9};
  
     for (std::vector<int>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)
+
     for (std::vector<int>::const_reverse_iterator i{v.crbegin()}; i != v.crend(); ++i)
 
         std::cout << *i << ' ';
 
         std::cout << *i << ' ';
 
     std::cout << '\n';
 
     std::cout << '\n';

Revision as of 07:59, 9 July 2023

 
 
C++ named requirements
 

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

Requirements

X Container type
T Element type
a Objects of type X

Types

Expression Return type Conditions
X::reverse_iterator Iterator type whose value type is T reverse_iterator<iterator>
X::const_reverse_iterator Constant iterator type whose value type is T reverse_iterator<const_iterator>

Member functions

Expression Return type Conditions Complexity
a.rbegin() reverse_iterator; const_reverse_iterator for constant a reverse_iterator(end()) Constant
a.rend() reverse_iterator; const_reverse_iterator for constant a reverse_iterator(begin()) Constant
a.crbegin() const_reverse_iterator const_cast<X const&>(a).rbegin() Constant
a.crend() const_reverse_iterator const_cast<X const&>(a).rend() Constant

Standard library

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