//find_first_of2a.cpp

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

/**
Tests if two positive integers have the same digit sum.
Pre:\n i1 and i2 have been initialized and i1, i2 are both > 0.
Post:\n Returns true if sum of the digits in i1 is == sum of digits
in i2, and otherwise returns false.
*/
bool digitSumsAreSame
(
    int i1, //in
    int i2  //in
)
{
    int digitSum1 = 0;
    while (i1 != 0)
    {
        digitSum1 += i1 % 10;
        i1 /= 10;
    }

    int digitSum2 = 0;
    while (i2 != 0)
    {
        digitSum2 += i2 % 10;
        i2 /= 10;
    }
    return digitSum1 == digitSum2;
}

int main()
{
    cout << "\nThis program illustrates the use of the STL "
        "find_first_of() algorithm (extended\nversion) to find the first "
        "occurrence of any one of a range of integer values\nin a vector "
        "within another range of integer values, also in a vector, and "
        "where\ncorresponding values match if and only if they have the "
        "same digit sum.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    int a1[] = {17, 5, 13, 21, 6, 7, 8, 14, 9, 10, 11};
    vector<int> v1(a1, a1+11);
    cout << "\nHere are the contents of v1:\n";
    for (vector<int>::size_type i=0; i<v1.size(); i++)
        cout << v1.at(i) << " ";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    int a2[] = {12, 41, 19};
    vector<int> v2(a2, a2+3);
    cout << "\nHere are the contents of v2:\n";
    for (vector<int>::size_type i=0; i<v2.size(); i++)
        cout << v2.at(i) << " ";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    vector<int>::iterator p;

    p = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(),
        digitSumsAreSame);
    if (p != v1.end())
        cout << "\nThe first instance of a value in v2 matching a "
            "value in v1\nhappens at location "
            << (int)(p-v1.begin()+1) << ".";
    else
        cout << "\nNo instance of a value in v2 matching a value "
            "in v1 was found.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(),
        digitSumsAreSame);
    if (p != v1.end())
        cout << "\nThe next instance of a value in v2 matching a "
            "value in v1\nhappens at location "
            << (int)(p-v1.begin()+1) << ".";
    else
        cout << "\nNo further instance of a value in v2 matching a "
            "value in v1 was found.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(),
        digitSumsAreSame);
    if (p != v1.end())
        cout << "\nThe next instance of a value in v2 matching a "
            "value in v1\nhappens at location "
            << (int)(p-v1.begin()+1) << ".";
    else
        cout << "\nNo further instance of a value in v2 matching a "
            "value in v1 was found.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

    p = find_first_of(p+1, v1.end(), v2.begin(), v2.end(),
        digitSumsAreSame);
    if (p != v1.end())
        cout << "\nThe next instance of a value in v2 matching a "
            "value in v1\nhappens at location "
            << (int)(p-v1.begin()+1) << ".";
    else
        cout << "\nNo further instance of a value in v2 matching a "
            "value in v1 was found.";
    cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
}
