//partition1a.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 partition() "
        "algorithm to\npartition the integer values in a vector of "
        "integers into two groups:\nthose that are divisible by 3, and "
        "those that are not.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    int a[] ={11, 7, 9, 4, 8, 12, 2, 5, 3, 10, 1, 6};
    vector<int> v(a, a+12);
    cout << "\nHere are the contents of the vector:\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');

    vector<int>::iterator p = partition(v.begin(), v.end(),
        isDivisibleByThree);
    cout << "\nAnd here are the contents of the partitioned vector:\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 << "\nThe iterator p returned by the algorithm points to ";
    if (p == v.end())
        cout << "\nthe end of the ouput container.";
    else
        cout << "the value " << *p << ".";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
}
