Answer:

// Heading
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {

    // Declarations
    vector<int> V1;
    vector<int> V2;
    int i;

    // Set the capacity of V1
    V1.reserve(20);

    // Print technical information
    cout << "Max_size = " << V1.max_size() << endl;
    cout << "Size = " << V1.size() << endl;
    cout << "Capacity = " << V1.capacity() << endl;
    cout << endl;

    // Assignment of V1
    for (i=1;i<=3;++i){
        V1.push_back(i);
    }
    for (i=1;i<=3;++i){
        V1.push_back(i);
    }

    // Re-print technical information
    cout << "Max_size = " << V1.max_size() << endl;
    cout << "Size = " << V1.size() << endl;
    cout << "Capacity = " << V1.capacity() << endl;

    cout << endl;

    // Remove the elements having the value "2" out of V1
    V1.erase (remove(V1.begin(),V1.end(),2),V1.end());

    // Assignment of the V1-entries to V2
    V2=V1;

    // Declaration of the iterator "pos"
    vector<int> :: iterator pos;

    // Modification of V2 so that "V2=(V1)^2"
    for (pos=V2.begin();pos!=V2.end();++pos) {
        *pos=(*pos)*(*pos);
    }
    // Caution: the loop:  
for (pos=V2.begin();pos!=4;++pos) doesn't work !

    // Print V1
    cout << "V1 = ";
    for (pos=V1.begin();pos!=V1.end();++pos) {
        cout << *pos << ' ';
    }
    cout << endl;

    // Other command for printing V1 (dangerous)
   
for (i=0;i<4;++i) {
        cout << V1[i] << ' ';
    }

    cout << endl;

    // Declaration of the reverse iterator rpos
    vector<int> :: reverse_iterator rpos;

    // Print V2 in the reverse order
    cout << "Reverse V2 = ";
    for (rpos=V2.rbegin();rpos!=V2.rend();++rpos) {
        cout << *rpos << ' ';
    }
     cout << endl;


}


Return to the container main page