Let's look at a simple example to illustrate vectors and a few of the member functions.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
// vectors are parameterized types
vector <double> junk1(10); // allocate 10 double elements, initialize all to 0.0
vector <int> junk2(10, 7); // allocate 10 int elements, initialize all to 7
vector <int> grades; // no size specified; contains zero elements
int num_students, i;
cout << "Size of junk1 is " << junk1.size() << ".\n";
cout << "Size of junk2 is " << junk2.size() << ".\n";
cout << "Size of grades is " << grades.size() << ".\n" << endl;
cout << "How many students are in CS 102? ";
cin >> num_students;
// set the size at run time with member function resize()
grades.resize(num_students);
cout << "Size of grades is " << grades.size() << ".\n" << endl;
// fill vector grades, use [] to access individual elements
for(i = 0; i < num_students; i++) {
cout << "enter grade for student number " << i+1 << ": ";
cin >> grades[i];
}
// resize vector at run time; new size can be constant, expr., or variable
grades.resize(1000);
cout << endl << "Size of grades is " << grades.size() << ".\n";
grades.resize(num_students + 1);
cout << "Size of grades is " << grades.size() << ".\n";
grades.resize(grades[2]);
cout << "Size of grades is " << grades.size() << ".\n";
// different example; declare new vectors; *** DON'T DO THIS***
vector <int> test1 (80, -1); // initialize 80 elements to to -1
vector <int> test2 (85); // automatically set 85 elements to 0
vector <int> count (101); // automatically set 101 elements to 0
vector <double> average (101); // automatically set 101 elements to 0.0
// assign values in vector test1 to vector test2; truncate test2 to 80 elements
test2 = test1;
cout << "\nsize of test2: " << test2.size() << endl;
// assign values in count to test2; expand test2 to 101 elements
test2 = count;
cout << "\nsize of test2: " << test2.size() << endl << endl;
// Are the two vectors equal in size and element-by-element value?
if(test1 != test2) cout << "\nVectors are not equal.\n\n";
// Member function at() returns a reference in the vector and is safer than the [] operator
// because at() won't let you reference items outside the bounds of the vector.
// Example: It is safer to use test1.at(i) than it is to use test1[i].
// Example: use at member function to check array boundary - does test1[1000] exist?
// If not, abort with runtime error
test1.at(1000) = 200;
cout << "We will never reach this point if there is no test1[1000].\n";
return 0;
}
|
OUTPUT:
Size of junk1 is 10.
Size of junk2 is 10.
Size of grades is 0.
How many students are in CS 102? 5
Size of grades is 5.
enter grade for student number 1: 100
enter grade for student number 2: 96
enter grade for student number 3: 82
enter grade for student number 4: 75
enter grade for student number 5: 90
Size of grades is 1000.
Size of grades is 6.
Size of grades is 82.
size of test2: 80
size of test2: 101
Vectors are not equal.
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
zsh: abort a.out
|
#include<iostream>
#include<vector>
using namespace std;
void func(int a[], vector <int> v)
{
int i;
v[2] = a[0] + v[1];
a[2] = v[2] % a[1];
v[0] = v[2] / a[1];
cout << "In func(): \n";
for(i = 0; i < 3; i++)
cout << a[i] << " ";
cout << endl;
for(i = 0; i < 3; i++)
cout << v[i] << " ";
cout << endl << endl;
}
int main(void)
{
int a[3] = {10, 11, 12};
vector |
Output: Size of vector: 3 In func(): 10 11 9 2 21 31 In main(): 10 11 9 20 21 22 |