Re: Alternative STL Structure?
mrc2323@cox.net (Mike Copeland) wrote in
news:MPG.27631aa63a0fcdac98969b@news.eternal-september.org:
In article <6ad83420-2641-40ce-98a5-61e709242137@
37g2000prx.googlegroups.com>, xtrigger303@gmail.com says...
On 1 Dic, 05:01, Ian Collins <ian-n...@hotmail.com> wrote:
On 12/ 1/10 04:52 PM, Mike Copeland wrote:
In article<278d307f-721a-453d-a36d-4a0903e6ca33
@d8g2000yqf.googlegroups.com>, james.ka...@gmail.com says...
I have the following data structure that is currently
process=
ed by an
STL map.
Although it currently works for most storage and
retrieval, I=
have
need to process the data in different orders than the map key
(teamNa=
me,
teamTypeCode). Knowing that I can't "resort" the data in an
STL ma=
p, is
there another STL structure that I could use to store, sort,
retrieve and process the data defined in this structure? TIA
The code you posted for me doesn't compile (VS6.0), and I don't
know
what to do to move forward with your help. Specifically, the main
errors (there are 13) are highlighted with comments before the code.
Please advise. TIA
The advice is to abandon VS6 and switch over to a proper C++ compiler.
There are also free versions of VS2010 available as I've heard.
Alternatively, if the compiler does not like some template syntax you
always have the option to manually generate the templated funtion for
each needed specialization. It's easy in this example as there are only 2
specializations (for m2 and m3), see below:
#pragma warning (disable:4786) // I added this line
#include <set>
#include <iostream>
#include <iomanip>
#include <algorithm> // needed for std::find
#include <string> // needed for std::cout << std::string
//----------------------------------------------------------------------
class CData
{
public:
int m1;
int m2;
int m3;
// normal "full" ordering; notice m3 is not taken into account
bool operator<(CData const & inObj) const
{
return m1 < inObj.m1 ? true : inObj.m1 < m1 ? false :
m2 < inObj.m2 ? true : false;
}
};
std::ostream & operator<<(std::ostream & inStream, CData const & inData)
{
return inStream << std::setw(4) << inData.m1 << " "
<< std::setw(4) << inData.m2 << " "
<< std::setw(4) << inData.m3 << std::endl;
}
typedef std::multiset<CData> CMasterSet;
typedef CMasterSet::iterator CMasterIter;
struct CIterComp_m2
{
// custom ordering giving priority to one member (m2)
// then taking into account normal ordering
bool operator()(CMasterIter const & in1, CMasterIter const & in2)
const
{ return ((*in1).m2) < ((*in2).m2) ? true :
((*in2).m2) < ((*in1).m2) ? false :
*in1 < *in2;
}
};
struct CIterComp_m3
{
// custom ordering giving priority to one member (m3)
// then taking into account normal ordering
bool operator()(CMasterIter const & in1, CMasterIter const & in2)
const
{ return ((*in1).m3) < ((*in2).m3) ? true :
((*in2).m3) < ((*in1).m3) ? false :
*in1 < *in2;
}
};
typedef std::multiset< CMasterIter,
CIterComp_m2> CSlaveSet2;
typedef std::multiset< CMasterIter,
CIterComp_m3> CSlaveSet3;
//////////////////////////////////////////////////////////
int main()
{
CMasterSet masterSet;
CSlaveSet2 slaveSet2;
CSlaveSet3 slaveSet3;
// populate
for(int c = 0; c < 15; ++c)
{
CData data;
data.m1 = static_cast< double >(rand()) / RAND_MAX * 20;
data.m2 = static_cast< double >(rand()) / RAND_MAX * 20;
data.m3 = static_cast< double >(rand()) / RAND_MAX * 20;
CMasterIter iter = masterSet.insert(data);
slaveSet2.insert(iter);
slaveSet3.insert(iter);
}
// erasing is tricky
{
CData dummy;
dummy.m1 = 10;
CMasterIter iter = masterSet.lower_bound(dummy);
// suppose you have an iterator into the master set
// you want to erase THAT item from all sets
// doing slaveSet2.erase(iter) might not do what you want
// while std::find will use equality comparison
// between iterators
// ah... erase first from slaves or else... UB
////////////////////////////////////////////////////////////////
// More errors:
// error C2065: 'find' : undeclared identifier
// error C2039: 'find' : is not a member of 'std'
////////////////////////////////////////////////////////////////
slaveSet2.erase(
std::find(slaveSet2.lower_bound(iter),
slaveSet2.end(), iter));
slaveSet3.erase(
std::find(slaveSet3.lower_bound(iter),
slaveSet3.end(), iter));
masterSet.erase(iter);
}
// DEFAULT ORDERING
for(CMasterIter iter = masterSet.begin();
iter != masterSet.end(); ++iter)
std::cout << *iter;
std::cout << std::string(20, '-') << std::endl;
// SECOND ORDERING
for(CSlaveSet2::iterator iter = slaveSet2.begin();
iter != slaveSet2.end(); ++iter)
std::cout << **iter;
std::cout << std::string(20, '-') << std::endl;
// THIRD ORDERING
for(CSlaveSet3::iterator iter = slaveSet3.begin();
iter != slaveSet3.end(); ++iter)
std::cout << **iter;
return 0;
}
// END CODE