7.10. Introduction to C++ Standard Library Class Template vector

We now introduce C++ Standard Library class template vector, which is similar to class template array, but also supports dynamic resizing. Except for the features that modify a vector, the other features shown in Fig. 7.25 also work for arrays. Standard class template vector is defined in header <vector> (line 5) and belongs to namespace std. Chapter 15 discusses the full functionality of vector. At the end of this section, we’ll demonstrate class vector’s bounds checking capabilities and introduce C++’s exception-handling mechanism, which can be used to detect and handle an out-of-bounds vector index.


 1   // Fig. 7.25: fig07_25.cpp
 2   // Demonstrating C++ Standard Library class template vector.
 3   #include <iostream>
 4   #include <iomanip>
 5   #include <vector>   
 6   #include <stdexcept>
 7   using namespace std;
 8
 9   void outputVector( const vector< int > & ); // display the vector
10   void inputVector( vector< int > & ); // input values into the vector
11
12   int main()
13   {
14      vector< int > integers1( 7 ); // 7-element vector< int >  
15      vector< int > integers2( 10 ); // 10-element vector< int >
16
17      // print integers1 size and contents
18      cout << "Size of vector integers1 is " << integers1.size()
19         << " vector after initialization:" << endl;
20      outputVector( integers1 );
21
22      // print integers2 size and contents
23      cout << " Size of vector integers2 is " << integers2.size()
24         << " vector after initialization:" << endl;
25      outputVector( integers2 );
26
27      // input and print integers1 and integers2
28      cout << " Enter 17 integers:" << endl;
29      inputVector( integers1 );
30      inputVector( integers2 );
31
32      cout << " After input, the vectors contain: "
33         << "integers1:" << endl;
34      outputVector( integers1 );
35      cout << "integers2:" << endl;
36      outputVector( integers2 );
37
38      // use inequality (!=) operator with vector objects
39      cout << " Evaluating: integers1 != integers2" << endl;
40
41      if ( integers1 != integers2 )
42         cout << "integers1 and integers2 are not equal" << endl;
43
44      // create vector integers3 using integers1 as an         
45      // initializer; print size and contents                  
46      vector< int > integers3( integers1 ); // copy constructor
47
48      cout << " Size of vector integers3 is " << integers3.size()
49         << " vector after initialization:" << endl;
50      outputVector( integers3 );
51
52      // use overloaded assignment (=) operator              
53      cout << " Assigning integers2 to integers1:" << endl; 
54      integers1 = integers2; // assign integers2 to integers1
55
56      cout << "integers1:" << endl;
57      outputVector( integers1 );
58      cout << "integers2:" << endl;
59      outputVector( integers2 );
60
61      // use equality (==) operator with vector objects
62      cout << " Evaluating: integers1 == integers2" << endl;
63
64      if ( integers1 == integers2 )
65         cout << "integers1 and integers2 are equal" << endl;
66
67      // use square brackets to use the value at location 5 as an rvalue
68      cout << " integers1[5] is " << integers1[ 5 ];
69
70      // use square brackets to create lvalue
71      cout << " Assigning 1000 to integers1[5]" << endl;
72      integers1[ 5 ] = 1000;
73      cout << "integers1:" << endl;
74      outputVector( integers1 );
75
76      // attempt to use out-of-range subscript                     
77      try                                                          
78      {                                                            
79         cout << " Attempt to display integers1.at( 15 )" << endl;
80         cout << integers1.at( 15 ) << endl; // ERROR: out of range
81      } // end try                                                 
82      catch ( out_of_range &ex )                                   
83      {                                                            
84         cerr << "An exception occurred: " << ex.what() << endl;   
85      } // end catch                                               
86
87      // changing the size of a vector
88      cout << " Current integers3 size is: " << integers3.size() << endl;
89      integers3.push_back( 1000 ); // add 1000 to the end of the vector
90      cout << "New integers3 size is: " << integers3.size() << endl;
91      cout << "integers3 now contains: ";
92      outputVector( integers3 );
93   } // end main
94
95   // output vector contents
96   void outputVector( const vector< int > &array )
97   {
98      for ( int item : items )
99         cout << item << " ";
100
101     cout << endl;
102  } // end function outputVector
103
104  // input vector contents
105  void inputVector( vector< int > &array )
106  {
107     for ( int &item : items )
108        cin >> item;
109  } // end function inputVector


Size of vector integers1 is 7
vector after initialization:
0 0 0 0 0 0 0

Size of vector integers2 is 10
vector after initialization:
0 0 0 0 0 0 0 0 0 0

Enter 17 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

After input, the vectors contain:
integers1:
1 2 3 4 5 6 7
integers2:
8 9 10 11 12 13 14 15 16 17

Evaluating: integers1 != integers2
integers1 and integers2 are not equal

Size of vector integers3 is 7
vector after initialization:
1 2 3 4 5 6 7

Assigning integers2 to integers1:
integers1:
8 9 10 11 12 13 14 15 16 17
integers2:
8 9 10 11 12 13 14 15 16 17

Evaluating: integers1 == integers2
integers1 and integers2 are equal

integers1[5] is 13

Assigning 1000 to integers1[5]
integers1:
8 9 10 11 12 1000 14 15 16 17

Attempt to display integers1.at( 15 )
An exception occurred: invalid vector<T> subscript

Current integers3 size is: 7
New integers3 size is: 8
integers3 now contains: 1 2 3 4 5 6 7 1000


Fig. 7.25. Demonstrating C++ Standard Library class template vector.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.237.3