//remove_if1a.cpp

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

/**
Determines if an integer is divisible by 3.
Pre:\n n contains an integer.
Post:\n Returns true if n is divisible by 3, and otherwise false.
*/
bool isDivisibleByThree
(
    int n //in
)
{
    return (n%3 == 0);
}

int main()
{
    cout << "\nThis program illustrates the use of the STL remove_if() "
        "algorithm to\nremove all values that are divisible by 3 from "
        "a vector of integers.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    int a[] = {1, 2, 2, 3, 4, 5, 2, 3, 6, 7, 2, 8, 3, 9, 10, 2};
    vector<int> v(a, a+16);

    cout << "\nHere are the sixteen values in the vector:\n";
    for (vector<int>::size_type i=0; i<v.size(); i++)
        cout << v.at(i) << " ";
    cout << "\nSize of vector to start:     " << v.size();
    cout << "\nCapacity of vector to start: " << v.capacity();
    cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

    cout << "\nNow we remove all 5 values that are divisble by 3.";
    cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
    vector<int>::iterator new_end = remove_if(v.begin(), v.end(),
        isDivisibleByThree);
    cout << "\nSize of vector immediately after "
        "the call to remove_if:     " << v.size();
    cout << "\nCapacity of vector immediately after "
        "the call to remove_if: " << v.capacity();
    cout << "\nContents of vector immediately after "
        "the call to remove_if:\n";
    for (vector<int>::size_type i=0; i<v.size(); i++)
        cout << v.at(i) << " ";
    cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

    cout << "\nNow we erase the last five values of the vector.";
    cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
    v.erase(new_end, v.end());
    cout << "\nSize of vector immediately after the call to erase:     "
        << v.size();
    cout << "\nCapacity of vector immediately after the call to erase: "
        << v.capacity();
    cout << "\nContents of vector immediately after the call to erase:\n";
    for (vector<int>::size_type i=0; i<v.size(); i++)
        cout << v.at(i) << " ";
    cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
}
