Difference between revisions of "cpp/named req/ReversibleContainer"
From cppreference.com
m (link to ja) |
m (reorder, fmt #Example) |
||
Line 37: | Line 37: | ||
|} | |} | ||
+ | ===Standard library=== | ||
+ | * {{lc|std::array}} | ||
+ | * {{lc|std::deque}} | ||
+ | * {{lc|std::list}} | ||
+ | * {{lc|std::vector}} | ||
+ | * {{lc|std::map}} | ||
+ | * {{lc|std::multimap}} | ||
+ | * {{lc|std::set}} | ||
+ | * {{lc|std::multiset}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | | The following example iterates over a vector (which has random-access iterators) in reverse. | + | | The following example iterates over a {{lc|std::vector|vector}} (which has [[cpp/container/vector#Member types|random-access iterators]]) in reverse. |
| code= | | code= | ||
#include <vector> | #include <vector> | ||
Line 50: | Line 59: | ||
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= | | output= | ||
− | 9 | + | 9 5 1 4 1 3 |
− | 5 | + | |
− | 1 | + | |
− | 4 | + | |
− | 1 | + | |
− | 3 | + | |
}} | }} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 17:10, 17 November 2021
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 |
Methods
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 <vector> #include <iostream> 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