Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
vector.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cout, endl
30#include <iterator> // for ostream_iterator
31#include <vector> // for vector
32
33#include <examples.h>
34
35
36// streams out a vector
37template <class T>
38std::ostream&
39operator<< (std::ostream &out, const std::vector<T, std::allocator<T> > &v)
40{
41 typedef std::ostream_iterator<T, char, std::char_traits<char> > Iter;
42
43 std::copy (v.begin (), v.end (), Iter (out, " "));
44
45 return out;
46}
47
48
49int main ()
50{
51 const int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
52
53 typedef std::vector<int, std::allocator<int> > IntVector;
54 typedef std::vector<double, std::allocator<double> > DblVector;
55
56 // demonstrate the initialization a vector of one type
57 // from a range of of values of another type denoted by
58 // either a pair of pointers or iterators
59 const IntVector vi (a, a + sizeof a / sizeof *a);
60 DblVector vd (vi.begin (), vi.end ());
61
62 // print out the two vectors
63 std::cout.setf (std::cout.showpoint);
64 std::cout.precision (2);
65
66 std::cout << vi << '\n' << vd << '\n';
67
68 // overwrite the second vector with the same values
69 // but of a different type
70 const IntVector::size_type mid = vi.size () / 2;
71
72 // demonstrate the assignment to a vector of one type
73 // from a range of values of another type denoted by
74 // a pair of iterators
75 vd.assign (vi.begin (), vi.begin () + mid);
76
77 // demonstrate the insertion into a vector of one type
78 // from a range of values of another type denoted by
79 // a pair of pointers
80 vd.insert (vd.end (), a + mid, a + sizeof a / sizeof *a);
81
82 // print out the second vector
83 std::cout << vd << '\n';
84
85 return 0;
86}
#define T
Definition 17.names.cpp:163
#define v
Definition 17.names.cpp:138
#define a
Definition 17.names.cpp:109
#define showpoint
const int out
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
std::ostream & operator<<(std::ostream &out, const std::vector< T, std::allocator< T > > &v)
Definition vector.cpp:39
int main()
Definition vector.cpp:49