#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define target 5
vector<vector<int>> find_twosum(vector<int> &v)
{
sort(v.begin(), v.end());
auto beg = v.begin();
auto last = v.end() - 1;
vector<int> a(2);
vector<vector<int>> q;
a.clear();
while (beg != last && last != v.end())
{
int sum = (*beg) + (*last);
if (sum == target)
{
a.push_back(*beg);
a.push_back(*last);
q.push_back(a);
a.clear();
++beg;
if (last == v.end() - 1)
{
last = last;
}
else
++last;
}
else if (sum < target)
{
++beg;
}
else
--last;
}
return q;
}
int main()
{
vector<int> v{ -1, -2, -3, 0, 1, 2, 3, 4 };
auto result = find_twosum(v);
for (auto it = result.begin(); it != result.end(); ++it)
{
for (auto beg = (*it).begin(); beg != (*it).end(); ++beg)
{
cout << *beg << " ";
}
cout << endl;
}
}
#include <vector>
#include <algorithm>
using namespace std;
#define target 5
vector<vector<int>> find_twosum(vector<int> &v)
{
sort(v.begin(), v.end());
auto beg = v.begin();
auto last = v.end() - 1;
vector<int> a(2);
vector<vector<int>> q;
a.clear();
while (beg != last && last != v.end())
{
int sum = (*beg) + (*last);
if (sum == target)
{
a.push_back(*beg);
a.push_back(*last);
q.push_back(a);
a.clear();
++beg;
if (last == v.end() - 1)
{
last = last;
}
else
++last;
}
else if (sum < target)
{
++beg;
}
else
--last;
}
return q;
}
int main()
{
vector<int> v{ -1, -2, -3, 0, 1, 2, 3, 4 };
auto result = find_twosum(v);
for (auto it = result.begin(); it != result.end(); ++it)
{
for (auto beg = (*it).begin(); beg != (*it).end(); ++beg)
{
cout << *beg << " ";
}
cout << endl;
}
}