とくにあぶなくないRiSKのブログ

危ないRiSKのブログだったかもしれない。本当はRiSKだけどググラビリティとか取得できるIDの都合でsscriskも使ったり。

begin, end / rbegin, rend

STL―標準テンプレートライブラリによるC++プログラミング 第2版

// P197 ex10-01.cpp
#include<iostream>
template<typename Container>
void display(const Container& c){
 using std::cout;
 using std::endl;
 {
 cout << "Elements in normal (forward) order: ";
 typename Container::const_iterator i = c.begin(),
  end = c.end();
 for(; i != end; ++i)cout << *i << ' ';
 cout << endl;
 }
 {
 cout << "Elements in reverse order:          ";
 typename Container::const_reverse_iterator i = c.rbegin(),
  end = c.rend();
 for(; i != end; ++i)cout << *i << ' ';
 cout << endl;
 }
}
#include<vector>
#include<list>
int main(){
 using std::cout;
 using std::vector;
 using std::list;
 cout << "Normal and reverse iteration through a vector:\n";
 vector<int> v;
 v.push_back(2);
 v.push_back(3);
 v.push_back(5);
 v.push_back(7);
 v.push_back(11);
 display(v);
 cout << "Normal and reverse iteration through a list:\n";
 list<int> l(v.begin(), v.end());
 display(l);
}

出力:

Normal and reverse iteration through a vector:
Elements in normal (forward) order: 2 3 5 7 11
Elements in reverse order:          11 7 5 3 2
Normal and reverse iteration through a list:
Elements in normal (forward) order: 2 3 5 7 11
Elements in reverse order:          11 7 5 3 2