Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
distance.cpp
Go to the documentation of this file.
1
29#include <cassert>
30#include <iterator>
31#include <iostream>
32#include <vector>
33
34#include <examples.h>
35
36
37int main ()
38{
39 // Typedefs for convenience.
40 typedef std::vector<int, std::allocator<int> > Vector;
41
42 typedef std::ostream_iterator<Vector::value_type,
43 char,
44 std::char_traits<char> > os_iter;
45
46 // Initialize a vector using an array.
47 const Vector::value_type arr [] = { 3, 4, 5, 6, 7, 8 };
48 const Vector v (arr + 0, arr + sizeof arr/ sizeof *arr);
49
50 // Declare a vector iterator, s.b. a ForwardIterator.
51 Vector::const_iterator it = v.begin () + 3;
52
53 // Output the original vector.
54 std::cout << "For the vector: ";
55 std::copy (v.begin (), v.end (), os_iter (std::cout, " "));
56 std::cout << '\n';
57
58 std::cout << "\nWhen the iterator is initialized to point to "
59 << *it << '\n';
60
61 // Compute the distance of it from the first element.
62 Vector::difference_type dist = 0;
63
64#ifndef _RWSTD_NO_EXT_VOID_DISTANCE
65
66 // using the extended overload of function template distance()
67 // provided by this implmentation, compute the distance between
68 // the two iterators
69 std::distance (v.begin (), it, dist);
70
71 assert (3 == dist);
72
73#endif // _RWSTD_NO_EXT_VOID_DISTANCE
74
75 // do the same using the standard function template distance()
76 dist = std::distance (v.begin (), it);
77
78 std::cout << "The distance between the beginning and "
79 << *it << " is " << dist << '\n';
80
81 return 0;
82}
#define v
Definition 17.names.cpp:138
#define dist
Definition 17.names.cpp:464
std::vector< UserClass, std::allocator< UserClass > > Vector
#define assert(expr)
Definition _cassert.h:40
int main()
Definition distance.cpp:37