Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
insert_iterator.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for copy
30#include <iostream> // for cout, endl
31#include <iterator> // for ostream_iterator, xxx_inserter
32#include <deque> // for deque
33
34#include <examples.h>
35
36
37int main ()
38{
39 // Typedefs for convenience.
40 typedef std::deque<int, std::allocator<int> > Deque;
41 typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter;
42
43 // Initialize a deque using an array.
44 Deque::value_type arr[] = { 3, 4, 7, 8 };
45 Deque d (arr, arr + sizeof arr / sizeof *arr);
46
47 // Output the original deque.
48 std::cout << "Start with a deque: \n ";
49 std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
50
51 // Insert into the middle.
52 std::insert_iterator<Deque> ins (d, d.begin () + 2);
53 *ins = 5;
54 *ins = 6;
55
56 // Output the new deque.
57 std::cout << "\n\nUse an insert_iterator: \n ";
58 std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
59
60 // A deque of four 1s.
61 Deque d2 (4, 1);
62
63 // Insert d2 at front of d.
64 std::copy (d2.begin (), d2.end (), std::front_inserter (d));
65
66 // Output the new deque.
67 std::cout << "\n\nUse a front_inserter: \n ";
68 std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
69
70 // Insert d2 at back of d.
71 std::copy (d2.begin (), d2.end (), std::back_inserter (d));
72
73 // Output the new deque.
74 std::cout << "\n\nUse a back_inserter: \n ";
75 std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
76
77 std::cout << std::endl;
78
79 return 0;
80}
#define d
Definition 17.names.cpp:115
#define d2
Definition 17.names.cpp:254
std::deque< std::string, std::allocator< std::string > > Deque
Definition deque.cpp:35
int main()