Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/ReversibleContainer"

From cppreference.com
< cpp‎ | named req
(not true for std::forward_list)
(elaborate requirements, clean up example)
Line 2: Line 2:
 
{{cpp/concept/navbar}}
 
{{cpp/concept/navbar}}
  
The concept {{concept|ReversibleContainer}} is a Forward Container whose iterators are Bidirectional Iterators. It allows backwards iteration through the container. It means that it can produce iterators that move backward from the end, as well as iterators that move forward from the beginning.
+
A {{ttb|ReversibleContainer}} is a {{concept|Container}} that has iterators that meet the requirements of either {{concept|BidirectionalIterator}} or {{concept|RandomAccessIterator}}.  Such iterators allow a {{ttb|ReversibleContainer}} to be iterated over in reverse.
 +
 
 +
===Requirements===
  
===Standard library===
 
The following standard library facilities.
 
 
{{dcl list begin}}
 
{{dcl list begin}}
{{dcl list template | cpp/container/dcl list rbegin | forward_list}}
+
{{dcl list item|{{ttb|X}}|Container type}}
{{dcl list template | cpp/container/dcl list rend | forward_list}}
+
{{dcl list item|{{ttb|T}}|Element type}}
 +
{{dcl list item|{{ttb|a}}, {{ttb|b}}|Objects of type {{ttb|X}}}}
 
{{dcl list end}}
 
{{dcl list end}}
 +
 +
====Types====
 +
 +
{|class=wikitable
 +
!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::const_reverse_iterator}}|| iterator type whose value type is {{tt|const T}} || {{c|reverse_iterator<const_iterator> }} || compile time
 +
|-
 +
|}
 +
 +
====Methods====
 +
 +
{|class=wikitable
 +
!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.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.crend()}}|| {{c|const_reverse_iterator}} || {{c|const_cast<X const&>(a).rend();}} || constant
 +
|-
 +
|}
 +
  
 
===Example===
 
===Example===
 
{{example
 
{{example
  | The following example uses vector but will work with all containers that support iteration.
+
  | The following example iterates over a vector (which has random-access iterators) in reverse.
 
  | code=
 
  | code=
#include <fstream>
 
#include <iostream>
 
#include <string>
 
 
#include <vector>
 
#include <vector>
#include "../require.h"
+
#include <iostream>
  
using namespace std;
+
int main()
 +
{
 +
    std::vector<int> v = {3, 1, 4, 1, 5, 9};
  
int main() {
+
     for(std::vector<int>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i) {
    ifstream in("Reversible.cpp");
+
         std::cout << *i << '\n';
    assure(in, "Reversible.cpp");
+
    }
    string line;
+
}
    vector<string> lines;
+
| output=
    while(getline(in, line))
+
9
        lines.push_back(line);
+
5
     for(vector<string>::reverse_iterator r = lines.rbegin();
+
1
        r != lines.rend(); r++)
+
4
         cout << *r << endl;
+
1
} | }}
+
3
 +
}}

Revision as of 07:16, 24 December 2012

Template:cpp/concept/title Template:cpp/concept/navbar

A ReversibleContainer is a Template:concept that has iterators that meet the requirements of either Template:concept or Template:concept. Such iterators allow a ReversibleContainer to be iterated over in reverse.

Contents

Requirements

X Container type
T Element type
a, b 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 iterator type whose value type is const 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


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 << '\n';
    }
}

Output:

9
5
1
4
1
3