Re: Alternative STL Structure?
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
// CODE
#pragma warning (disable:4786) // I added this line
#include <set>
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------
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;
//----------------------------------------------------------------------
template< typename T, T CData::*KPtr >
struct CIterComp
{
// custom ordering giving priority to one member
// then taking into account normal ordering
bool operator()(CMasterIter const & in1, CMasterIter const & in2)
const
{ return ((*in1).*KPtr) < ((*in2).*KPtr) ? true :
((*in2).*KPtr) < ((*in1).*KPtr) ? false :
*in1 < *in2;
}
};
//////////////////////////////////////////////////////////
// Errors below:
// error C2964: invalid expression as template parameter
//////////////////////////////////////////////////////////
typedef std::multiset< CMasterIter,
CIterComp< int, &CData::m2 > > CSlaveSet2;
typedef std::multiset< CMasterIter,
CIterComp< int, &CData::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