Difference between revisions of "cpp/named req/ReversibleContainer"
From cppreference.com
Andreas Krug (Talk | contribs) 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|{{ | + | {{dsc|{{tt|X}}|Container type}} |
− | {{dsc|{{ | + | {{dsc|{{tt|T}}|Element type}} |
− | {{dsc|{{ | + | {{dsc|{{c|a}}|Objects of type {{tt|X}}}} |
{{dsc end}} | {{dsc end}} | ||
====Types==== | ====Types==== | ||
{|class=wikitable | {|class=wikitable | ||
− | ! | + | !Expression||Return type||Conditions |
|- | |- | ||
− | |{{c|X::reverse_iterator}}|| | + | |{{c|X::reverse_iterator}}||Iterator type whose value type is {{tt|T}}||{{c|reverse_iterator<iterator>}} |
|- | |- | ||
− | |{{c|X::const_reverse_iterator}}|| | + | |{{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 |
|- | |- | ||
− | |{{c|a.rbegin()}}||{{c|reverse_iterator}}; {{c|const_reverse_iterator}} for 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 {{ | + | |{{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()}}|| | + | |{{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()}}|| | + | |{{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>:: | + | 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
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.
Run this code
#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