Przejdź do głównej zawartości

C++ named requirements: ReversibleContainer

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.

Requirements

  • X Container type
  • T Element type
  • a Objects of type X

Types

ExpressionReturn typeConditionsComplexity
X::reverse_iteratoriterator type whose value type is Treverse_iterator<iterator>compile time
X::const_reverse_iteratorconstant iterator type whose value type is Treverse_iterator<const_iterator>compile time

Methods

ExpressionReturn typeConditionsComplexity
a.rbegin()reverse_iterator; const_reverse_iterator for constant areverse_iterator(end())constant
a.rend()reverse_iterator; const_reverse_iterator for constant areverse_iterator(begin())constant
a.crbegin()const_reverse_iteratorconst_cast<X const&>(a).rbegin()constant
a.crend()const_reverse_iteratorconst_cast<X const&>(a).rend()constant

Standard library

Example

The following example iterates over a vector (which has random-access iterators) in reverse.

#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 << ' ';
}
}
Result
9 5 1 4 1 3

C++ named requirements: ReversibleContainer

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.

Requirements

  • X Container type
  • T Element type
  • a Objects of type X

Types

ExpressionReturn typeConditionsComplexity
X::reverse_iteratoriterator type whose value type is Treverse_iterator<iterator>compile time
X::const_reverse_iteratorconstant iterator type whose value type is Treverse_iterator<const_iterator>compile time

Methods

ExpressionReturn typeConditionsComplexity
a.rbegin()reverse_iterator; const_reverse_iterator for constant areverse_iterator(end())constant
a.rend()reverse_iterator; const_reverse_iterator for constant areverse_iterator(begin())constant
a.crbegin()const_reverse_iteratorconst_cast<X const&>(a).rbegin()constant
a.crend()const_reverse_iteratorconst_cast<X const&>(a).rend()constant

Standard library

Example

The following example iterates over a vector (which has random-access iterators) in reverse.

#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 << ' ';
}
}
Result
9 5 1 4 1 3