1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <array>
#include <iterator>
#include <iostream>
int main()
{
{
// an array of ten integers
const int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
auto current = std::begin(a) ; // a 'pointer' to the first element
auto beyond = std::end(a) ; // a 'pointer' to a non-existent one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
{
// a dynamically resizeable array of ten integers
const std::vector<int> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
auto current = a.begin() ; // a 'pointer' to the first element
auto beyond = a.end() ; // a 'pointer' to a non-existant one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
{
// a double ended queue of ten integers
const std::deque<int> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
auto current = a.begin() ; // a 'pointer' to the first element
auto beyond = a.end() ; // a 'pointer' to a non-existant one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
{
// a doubly linked list of ten integers
const std::list<int> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
auto current = a.begin() ; // a 'pointer' to the first element
auto beyond = a.end() ; // a 'pointer' to a non-existant one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
{
// a singly linked list of ten integers
const std::forward_list<int> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
auto current = a.begin() ; // a 'pointer' to the first element
auto beyond = a.end() ; // a 'pointer' to a non-existant one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
{
// a wrapper over an array of ten integers
std::array< const int, 10 > a = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } } ;
auto current = a.begin() ; // a 'pointer' to the first element
auto beyond = a.end() ; // a 'pointer' to a non-existant one beyond the last element
while( current != beyond ) // as long as we are not beyond the last element
{
std::cout << *current << ' ' ; // print out the element
++current ; // move current to 'point' to the next element
}
std::cout << '\n' ;
}
}
|