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)

Vector Element-Manipulation Functions

Figure 23.15 illustrates functions that enable retrieval and manipulation of the elements of a vector. Line 17 uses an overloaded vector constructor that takes two iterators as arguments to initialize integers. Remember that pointers into an array can be used as iterators. Line 17 initializes integers with the contents of array from location array up to—but not including—location array + SIZE.

 1  // Fig. 23.15: Fig23_15.cpp
 2  // Testing Standard Library vector class template
 3  // element-manipulation functions.
 4  #include <iostream>
 5  using std::cout;
 6  using std::endl;
 7
 8  #include <vector> // vector class-template definition
 9  #include <algorithm> // copy algorithm
10  #include <iterator> // ostream_iterator iterator
11  #include <stdexcept> // out_of_range exception
12
13  int main()
14  {
15     const int SIZE = 6;
16     int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 };
17     std::vector< int > integers( array, array + SIZE );
18     std::ostream_iterator< int > output( cout, " " );
19
20     cout << "Vector integers contains: ";
21     std::copy( integers.begin(), integers.end(), output );
22
23     cout << "\nFirst element of integers: " << integers.front()
24        << "\nLast element of integers: " << integers.back();
25
26     integers[ 0 ] = 7; // set first element to 7
27     integers.at( 2 ) = 10; // set element at position 2 to 10
28
29     // insert 22 as 2nd element
30     integers.insert( integers.begin() + 1, 22 );
31
32     cout << "\n\nContents of vector integers after changes: ";
33     std::copy( integers.begin(), integers.end(), output );
34
35     // access out-of-range element
36     try
37     {
38        integers.at( 100 ) = 777;
39     } // end try
40     catch ( std::out_of_range outOfRange ) // out_of_range exception
41     {
42        cout << "\n\nException: " << outOfRange.what();
43     } // end catch
44
45     // erase first element
46     integers.erase( integers.begin() );
47     cout << "\n\nVector integers after erasing first element: ";
48     std::copy( integers.begin(), integers.end(), output );
49
50     // erase remaining elements
51     integers.erase( integers.begin(), integers.end() );
52     cout << "\nAfter erasing all elements, vector integers ";
53        << ( integers.empty() ? "is" : "is not" ) << " empty";
54
55     // insert elements from array
56     integers.insert( integers.begin(), array, array + SIZE );
57     cout << "\n\nContents of vector integers before clear: ";
58     std::copy( integers.begin(), integers.end(), output );
59
60     // empty integers; clear calls erase to empty a collection
61     integers.clear();
62     cout << "\nAfter clear, vector integers "
63        << ( integers.empty() ? "is" : "is not" ) << " empty" << endl;
64     return 0;
65  } // end main
 Vector integers contains: 1 2 3 4 5 6
 First element of integers: 1
 Last element of integers: 6

 Contents of vector integers after changes: 7 22 2 10 4 5 6

 Exception: invalid vector<T> subscript

 Vector integers after erasing first element: 22 2 10 4 5 6
 After erasing all elements, vector integers is empty

 Contents of vector integers before clear: 1 2 3 4 5 6
 After clear, vector integers is empty

Fig. 23.15 vector class template element-manipulation functions.

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

Tutorial Index