Difference between revisions of "cpp/named req/ReversibleContainer"
From cppreference.com
m (reorder, fmt #Example) |
m (fmt) |
||
Line 13: | Line 13: | ||
====Types==== | ====Types==== | ||
− | |||
{|class=wikitable | {|class=wikitable | ||
!expression||return type||conditions||complexity | !expression||return type||conditions||complexity | ||
|- | |- | ||
− | |{{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>}}||compile time |
|- | |- | ||
− | |{{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>}}||compile time |
|- | |- | ||
|} | |} | ||
− | ==== | + | ====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 {{tt|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 {{tt|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 49: | Line 47: | ||
===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() | ||
Line 58: | Line 56: | ||
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>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i) |
std::cout << *i << ' '; | std::cout << *i << ' '; | ||
− | |||
} | } | ||
− | + | |output= | |
9 5 1 4 1 3 | 9 5 1 4 1 3 | ||
}} | }} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Revision as of 23:07, 8 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 | complexity |
---|---|---|---|
X::reverse_iterator | iterator type whose value type is T |
reverse_iterator<iterator> | compile time |
X::const_reverse_iterator | constant iterator type whose value type is T |
reverse_iterator<const_iterator> | compile time |
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>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i) std::cout << *i << ' '; }
Output:
9 5 1 4 1 3