Code:
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
struct MyPred
{
std::string a;
std::string x;
std::string y;
std::string z;
MyPred(const std::string& a, const std::string& x, const std::string& y, const std::string& z): a(a), x(x), y(y), z(z) {}
bool operator==(const MyPred& p) const
{
return x == p.x && y == p.y && z == p.z; // a == p.a &&
}
bool operator<(const MyPred& p) const
{
//if(a < p.a) return true;
//if(a > p.a) return false;
if(x < p.x) return true;
if(x > p.x) return false;
if(y < p.y) return true;
if(y > p.y) return false;
if(z < p.z) return true;
if(z > p.z) return false;
return false;
}
};
int main()
{
std::vector<MyPred>* vPred = new std::vector<MyPred>;
vPred->push_back(MyPred("a2c", "1Gak", "c", "d4f"));
vPred->push_back(MyPred("j4h", "b", "c", "j87h"));
vPred->push_back(MyPred("d4f", "1Gak", "c", "d4f"));
vPred->push_back(MyPred("n7s", "1Gak", "c", "d4f"));
vPred->push_back(MyPred("l9m", "b", "c", "j87h"));
vPred->push_back(MyPred("p24a", "x", "c", "p43a"));
vPred->push_back(MyPred("q56r", "l", "m", "q90r"));
vPred->push_back(MyPred("g11v", "8f", "h", "g63v"));
vPred->push_back(MyPred("u3w", "v", "d", "u11w"));
vPred->push_back(MyPred("k76l", "x", "c", "p43a"));
vPred->push_back(MyPred("p24a", "g", "z", "p43a"));
// 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 = new std::vector<MyPred>; // 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);
}*/
ret = std::equal_range(i, vPred->end(), *i);
if(ret.second - ret.first != 1) // duplicates
{
for(std::vector<MyPred>::iterator j = ret.first; j != ret.second; ++j)
{
dPred->push_back(*j); //put each duplicate onto a new vector
}
}
else if(ret.second - ret.first == 1)
{
uPred.push_back(*i);
}
}
std::cout << "vPred: Sorted input\n";
for(std::vector<MyPred>::iterator i = vPred->begin(); i != vPred->end(); ++i)
{
std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\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->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\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->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
}
delete vPred;
delete dPred;
char a;
std::cin >> a;
}