Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

 

23.2.1 vector Sequence Container (continued)

Using Vector and Iterators

Figure 23.14 illustrates several functions of the vector class template. Many of these functions are available in every first-class container. You must include header file <vector> to use class template vector.

 

 1  // Fig. 23.14: Fig23_14.cpp
 2  // Demonstrating Standard Library vector class template.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6
 7  #include <vector> // vector class-template definition
 8  using std::vector;
 9
10  // prototype for function template printVector
11  template < typename T > void printVector( const vector< T > &integers2 );
12
13  int main()
14  {
15     const int SIZE = 6; // define array size
16     int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; // initialize array
17     vector< int > integers; // create vector of ints
18
19     cout << "The initial size of integers is: " << integers.size()
20        << "\nThe initial capacity of integers is: " << integers.capacity();
21
22     // function push_back is in every sequence collection
23     integers.push_back( 2 );
24     integers.push_back( 3 );
25     integers.push_back( 4 );
26
27     cout << "\nThe size of integers is: " << integers.size()
28        << "\nThe capacity of integers is: " << integers.capacity();
29     cout << "\n\nOutput array using pointer notation: ";
30
31     // display array using pointer notation
32     for ( int *ptr = array; ptr != array + SIZE; ptr++ )
33        cout << *ptr << ' ';
34
35     cout << "\nOutput vector using iterator notation: ";
36     printVector( integers );
37     cout << "\nReversed contents of vector integers: ";
38
39     // two const reverse iterators
40     vector< int >::const_reverse_iterator reverseIterator;
41     vector< int >::const_reverse_iterator tempIterator = integers.rend();
42
43     // display vector in reverse order using reverse_iterator
44     for ( reverseIterator = integers.rbegin();
45        reverseIterator!= tempIterator; ++reverseIterator )
46        cout << *reverseIterator << ' ';
47
48     cout << endl;
49     return 0;
50  } // end main
51
52  // function template for outputting vector elements
53  template < typename T > void printVector( const vector< T > &integers2 )
54  {
55     typename vector< T >::const_iterator constIterator; // const_iterator
56
57     // display vector elements using const_iterator
58     for ( constIterator = integers2.begin();
59        constIterator != integers2.end(); ++constIterator )
60        cout << *constIterator << ' ';
61  } // end function printVector

 The initial size of integers is: 0

 The initial capacity of integers is: 0
 The size of integers is: 3
 The capacity of integers is: 4

 Output array using pointer notation: 1 2 3 4 5 6
 Output vector using iterator notation: 2 3 4
 Reversed contents of vector integers: 4 3 2
 

Fig. 23.14 Standard Library vector class template.

Page 1 | 2 | 3 | 4 | 5 | 6

Tutorial Index