1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
template <typename T>
void store(vector<T>& vec, T val)
{
vec.push_back(val);
}
bool nocase_compare(string s1, string s2)
{
transform(s1.begin(), s1.end(), s1.begin(), toupper);
transform(s2.begin(), s2.end(), s2.begin(), toupper);
return s1.compare(s2) == 0;
}
bool is_number(string s)
{
bool ret(s.length());
for (string::iterator itr = s.begin(); itr != s.end() && ret; ++itr)
ret = isdigit(*itr);
return ret;
}
template <typename T>
void locate_pairs(vector<T>& vec, const T target)
{
for (vector<T>::iterator itr = vec.begin(); itr != vec.end(); ++itr)
{
if (target - *itr > 0)
{
T n1 = *itr;
T n2 = target - *itr;
vector<T>::iterator f = itr;
f = find(++f, vec.end(), n2);
while (f != vec.end())
{
cout << "Pair: " << n1 << " " << n2 << " for target: " << target << endl;
f = find(++f, vec.end(), n2);
}
}
}
}
int main()
{
vector<int> v;
string input;
cout << "Enter a number or 'Y' to stop " << endl;
do
{
cin >> input;
if (is_number(input))
store<int>(v, atoi(input.c_str()));
} while (!nocase_compare(input, "Y"));
cout << "Enter a target number: ";
int target(0);
do
{
cin >> input;
if (is_number(input))
target = atoi(input.c_str());
} while (target == 0);
locate_pairs<int>(v, target);
return 0;
}
|