blob: 7fda7520406526165b2ce57b795c919a3a524284 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Fig. 8.14: fig08_14.cpp
// Demonstrating standard library class vector.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <vector>
using std::vector;
void outputVector( const vector< int > & );
void inputVector( vector< int > & );
int main()
{
vector< int > integers1( 7 ); // 7-element vector< int >
vector< int > integers2( 10 ); // 10-element vector< int >
// print integers1 size and contents
cout << "Size of vector integers1 is "
<< integers1.size()
<< "\nvector after initialization:\n";
outputVector( integers1 );
// print integers2 size and contents
cout << "\nSize of vector integers2 is "
<< integers2.size()
<< "\nvector after initialization:\n";
outputVector( integers2 );
// input and print integers1 and integers2
cout << "\nInput 17 integers:\n";
inputVector( integers1 );
inputVector( integers2 );
cout << "\nAfter input, the vectors contain:\n"
<< "integers1:\n";
outputVector( integers1 );
cout << "integers2:\n";
outputVector( integers2 );
// use overloaded inequality (!=) operator
cout << "\nEvaluating: integers1 != integers2\n";
if ( integers1 != integers2 )
cout << "integers1 and integers2 are not equal\n";
// create vector integers3 using integers1 as an
// initializer; print size and contents
vector< int > integers3( integers1 ); // copy constructor
cout << "\nSize of vector integers3 is "
<< integers3.size()
<< "\nvector after initialization:\n";
outputVector( integers3 );
// use overloaded assignment (=) operator
cout << "\nAssigning integers2 to integers1:\n";
integers1 = integers2;
cout << "integers1:\n";
outputVector( integers1 );
cout << "integers2:\n";
outputVector( integers1 );
// use overloaded equality (==) operator
cout << "\nEvaluating: integers1 == integers2\n";
if ( integers1 == integers2 )
cout << "integers1 and integers2 are equal\n";
// use overloaded subscript operator to create rvalue
cout << "\nintegers1[5] is " << integers1[ 5 ];
// use overloaded subscript operator to create lvalue
cout << "\n\nAssigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;
cout << "integers1:\n";
outputVector( integers1 );
// attempt to use out of range subscript
cout << "\nAttempt to assign 1000 to integers1.at( 15 )"
<< endl;
integers1.at( 15 ) = 1000; // ERROR: out of range
return 0;
} // end main
// output vector contents
void outputVector( const vector< int > &array )
{
for ( int i = 0; i < array.size(); i++ ) {
cout << setw( 12 ) << array[ i ];
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
cout << endl;
} // end for
if ( i % 4 != 0 )
cout << endl;
} // end function outputVector
// input vector contents
void inputVector( vector< int > &array )
{
for ( int i = 0; i < array.size(); i++ )
cin >> array[ i ];
} // end function inputVector
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
|