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
|
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
struct MyPred
{
std::string x;
std::string y;
MyPred(const std::string& x, const std::string& y): x(x), y(y) {}
bool operator==(const MyPred& p) const
{
return x == p.x && y == p.y;
}
bool operator<(const MyPred& p) const
{
if(x < p.x) return true;
if(x > p.x) return false;
if(y < p.y) return true;
if(y > p.y) return false;
return false;
}
};
int main()
{
std::vector<MyPred> vPred;
vPred.push_back(MyPred("a", "a"));
vPred.push_back(MyPred("a", "b"));
vPred.push_back(MyPred("a", "a"));
vPred.push_back(MyPred("b", "a"));
vPred.push_back(MyPred("a", "b"));
vPred.push_back(MyPred("b", "b"));
// The values need to be in order for equal_range() to work
std::sort(vPred.begin(), vPred.end());
std::vector<MyPred> uPred; // values that were always unique
std::vector<MyPred> dPred; // values that were duplicated
std::pair<std::vector<MyPred>::iterator, std::vector<MyPred>::iterator> ret;
for(std::vector<MyPred>::iterator i = vPred.begin(); i != vPred.end(); i = ret.second)
{
ret = std::equal_range(i, vPred.end(), *i);
if(ret.second - ret.first == 1)
{
uPred.push_back(*i);
}
else
{
dPred.push_back(*i);
}
}
std::cout << "vPred: Sorted input\n";
for(std::vector<MyPred>::iterator i = vPred.begin(); i != vPred.end(); ++i)
{
std::cout << "[" << i->x << ", " << i->y << "]" << '\n';
}
std::cout << "dPred: Only the values that were duplicated\n";
for(std::vector<MyPred>::iterator i = dPred.begin(); i != dPred.end(); ++i)
{
std::cout << "[" << i->x << ", " << i->y << "]" << '\n';
}
std::cout << "uPred: Only the values that were unique\n";
for(std::vector<MyPred>::iterator i = uPred.begin(); i != uPred.end(); ++i)
{
std::cout << "[" << i->x << ", " << i->y << "]" << '\n';
}
}
|
vPred: Sorted input
[a, a]
[a, a]
[a, b]
[a, b]
[b, a]
[b, b]
dPred: Only the values that were duplicated
[a, a]
[a, b]
uPred: Only the values that were unique
[b, a]
[b, b] |