Difference between revisions of "cpp/named req/ReversibleContainer"
(Standard library) |
m (+inplace_vector.) |
||
(13 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ | + | {{cpp/named req/title|ReversibleContainer}} |
− | {{cpp/ | + | {{cpp/named req/navbar}} |
− | A {{ | + | A {{named req|ReversibleContainer}} is a {{named req|Container}} that has iterators that meet the requirements of either {{named req|BidirectionalIterator}} or {{named req|RandomAccessIterator}}. Such iterators allow a {{named req/core|ReversibleContainer}} to be iterated over in reverse. |
===Requirements=== | ===Requirements=== | ||
− | + | A type satisfies {{named req/core|ReversibleContainer}} if it satisfies {{named req|Container}}, its iterator type belongs to the bidirectional or random access [[cpp/iterator|iterator categories]] and, given the following types and values, the semantic and complexity requirements in the tables below are satisfied: | |
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc hitem|Type|Definition}} |
− | {{ | + | {{dsc|{{tt|X}}|an {{named req/core|ReversibleContainer}} type}} |
− | {{ | + | {{dsc|{{tt|T}}|the {{tt|value_type}} of {{tt|X}}}} |
− | {{ | + | {{dsc hitem|Value|Definition}} |
+ | {{dsc|{{c|a}}|a value of type {{tt|X}}}} | ||
+ | {{dsc end}} | ||
====Types==== | ====Types==== | ||
− | |||
{|class=wikitable | {|class=wikitable | ||
− | ! | + | !Name |
+ | !Type | ||
+ | !Requirements | ||
|- | |- | ||
− | |{{c|X::reverse_iterator}} | + | |{{c/core|typename X::reverse_iterator}} |
− | | | + | |{{c/core|std::reverse_iterator<X::iterator>}} |
− | + | |an iterator type whose [[cpp/iterator|value type]] is {{tt|T}} | |
|- | |- | ||
+ | |{{c/core|typename X::const_reverse_iterator}}{{nbsp}} | ||
+ | |{{c/core|std::reverse_iterator<X::const_iterator>}}{{nbsp}} | ||
+ | |a constant iterator type whose [[cpp/iterator|value type]] is {{tt|T}} | ||
|} | |} | ||
− | ==== | + | ====Expressions==== |
+ | The types {{tt|reverse_iterator}} and {{tt|const_reverse_iterator}} in the following table denote {{c/core|typename X::reverse_iterator}} and {{c/core|typename X::const_reverse_iterator}} respectively. | ||
{|class=wikitable | {|class=wikitable | ||
− | ! | + | !Expression |
+ | !Type | ||
+ | !Semantics | ||
+ | !{{nbsp}}Complexity{{nbsp}} | ||
|- | |- | ||
− | |{{c|a.rbegin()}} | + | |{{c|a.rbegin()}} |
+ | |{{tt|reverse_iterator}}<br>{{tt|const_reverse_iterator}} for constant {{c|a}} | ||
+ | |{{c|reverse_iterator(a.end())}} | ||
+ | |Constant | ||
|- | |- | ||
− | |{{c|a.rend()}} | + | |{{c|a.rend()}} |
+ | |{{tt|reverse_iterator}}<br>{{tt|const_reverse_iterator}} for constant {{c|a}} | ||
+ | |{{c|reverse_iterator(a.begin())}} | ||
+ | |Constant | ||
|- | |- | ||
− | |{{c|a.crbegin()}} | + | |{{c|a.crbegin()}} |
− | | | + | |{{tt|const_reverse_iterator}} |
− | + | |{{c|const_cast<const X&>(a).rbegin()}} | |
+ | |Constant | ||
|- | |- | ||
+ | |{{c|a.crend()}} | ||
+ | |{{tt|const_reverse_iterator}} | ||
+ | |{{c|const_cast<const X&>(a).rend()}} | ||
+ | |Constant | ||
|} | |} | ||
+ | ===Standard library=== | ||
+ | * {{lc|std::array}} | ||
+ | * {{lc|std::deque}} | ||
+ | * {{lc|std::list}} | ||
+ | * {{lc|std::vector}} | ||
+ | * {{lc|std::inplace_vector}} | ||
+ | * {{lc|std::map}} | ||
+ | * {{lc|std::multimap}} | ||
+ | * {{lc|std::set}} | ||
+ | * {{lc|std::multiset}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |The following example iterates over a {{lc|std::vector|vector}} (which has [[cpp/container/vector#Member types|random-access iterators]]) in reverse. | |
− | + | |code= | |
− | + | ||
#include <iostream> | #include <iostream> | ||
+ | #include <vector> | ||
int main() | int main() | ||
{ | { | ||
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'; | |
} | } | ||
− | + | |output= | |
− | 9 | + | 9 5 1 4 1 3 |
− | 5 | + | |
− | 1 | + | |
− | 4 | + | |
− | 1 | + | |
− | 3 | + | |
}} | }} | ||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=2105|std=C++98|before={{c/core|typename X::const_reverse_iterator}} was<br>required to be an iterator type of value type {{c/core|const T}}|after=required to be a constant<br>iterator type of value type {{tt|T}}}} | ||
+ | {{dr list end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 03:22, 5 September 2024
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
- std::array
- std::deque
- std::list
- std::vector
- std::inplace_vector
- std::map
- std::multimap
- std::set
- std::multiset
[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
|