struct Thing
{
int a,b,c;
};
std::ostream& operator << (std::ostream& os,
Thing t)
{
return os << "a=" << t.a << ", b="
<< t.b << ", c=" << t.c;
}
namespace SomeFunc_ns
{
struct disposable_ftr
{
Thing *p;
operator()(int x, int y)
{
return p[x].a<p[y].a
|| (p[x].a==p[y].a
&& p[x].b<p[y].b)
|| (p[x].a==p[y].a
&& p[x].b==p[y].b
&& p[x].c<p[y].c);
}
};
}
void SomeFunc(std::ostream& os)
{
using namespace SomeFunc_ns;
std::vector<Thing> lotsofthings;
for (int a=0; a<2; a++)
for (int b=0; b<2; b++)
for (int c=0; c<2; c++)
{
Thing t;
t.a=1-a; t.b=b; t.c=1-c;
lotsofthings.push_back(t);
}
disposable_ftr f;
f.p = &*lotsofthings.begin();
typedef
std::set<unsigned int, disposable_ftr> Set;
Set indexedthings(f);
for (unsigned int n=0;
n<lotsofthings.size(); n++)
indexedthings.insert(n);
os << "original order" << std::endl;
for (int n=0; n<lotsofthings.size(); n++)
{
os << "[" << n << "] "
<< lotsofthings[n] << std::endl;
}
os << std::endl
<< "index array order" << std::endl;
Set::iterator it = indexedthings.begin();
for (int n=0; it != indexedthings.end();
it++, n++)
{
os
<< "[" << n << ", " << *it << "] "
<< lotsofthings[*it] << std::endl;
}
os << std::endl;
}
End of Listing