N
Ninan
I have a map, with a composite key (a struct). I would like to delete
all the elements with a partial key match and may be do other things in
the future. The way I am doing it is as follows. I think this is not
very effecient. I am wondering is there a better way to do this
struct SLNetKey
{
string m_usrid;
string m_portfolio;
string m_symbol;
PorSide m_enSide;
bool operator = (const stLNetRowInfo *row);
};
typedef map<SLNetKey, SLNetData> MapLNet;
typedef map<SLNetKey, SLNetData>::iterator MapiterLNet;
class CLNetData
{
public:
int DelRowsByUser (const char *user);
private:
MapLNet m_Data;
};
int CLNetData:
elRowsByUser (const char *user)
{
MapiterLNet pREnditer = m_Data.end();
MapiterLNet pBeg = m_Data.end(), pEnd = m_Data.end();
string User = user;
bool bBeg = true;
for (MapiterLNet pRiter = m_Data.begin(); pRiter != pREnditer;
++pRiter) {
if (bBeg && pRiter->first.m_usrid == User)
{
bBeg = false;
pBeg = pRiter;
}
else if (!bBeg)
{
if (pRiter->first.m_usrid != User)
{
pEnd = pRiter;
break;
}
}
}
if (pBeg == m_Data.end())
return -1;
m_Data.erase (pBeg, pEnd);
return 0;
}
//Map comparator function, just for information
bool operator<(const SLNetKey& lhs, const SLNetKey& rhs)
{
if (lhs.m_usrid != rhs.m_usrid)
return lhs.m_usrid < rhs.m_usrid;
if (lhs.m_portfolio != rhs.m_portfolio)
return lhs.m_portfolio < rhs.m_portfolio;
if (lhs.m_symbol != rhs.m_symbol)
return lhs.m_symbol < rhs.m_symbol;
return (int)lhs.m_enSide < (int)rhs.m_enSide;
}
all the elements with a partial key match and may be do other things in
the future. The way I am doing it is as follows. I think this is not
very effecient. I am wondering is there a better way to do this
struct SLNetKey
{
string m_usrid;
string m_portfolio;
string m_symbol;
PorSide m_enSide;
bool operator = (const stLNetRowInfo *row);
};
typedef map<SLNetKey, SLNetData> MapLNet;
typedef map<SLNetKey, SLNetData>::iterator MapiterLNet;
class CLNetData
{
public:
int DelRowsByUser (const char *user);
private:
MapLNet m_Data;
};
int CLNetData:
{
MapiterLNet pREnditer = m_Data.end();
MapiterLNet pBeg = m_Data.end(), pEnd = m_Data.end();
string User = user;
bool bBeg = true;
for (MapiterLNet pRiter = m_Data.begin(); pRiter != pREnditer;
++pRiter) {
if (bBeg && pRiter->first.m_usrid == User)
{
bBeg = false;
pBeg = pRiter;
}
else if (!bBeg)
{
if (pRiter->first.m_usrid != User)
{
pEnd = pRiter;
break;
}
}
}
if (pBeg == m_Data.end())
return -1;
m_Data.erase (pBeg, pEnd);
return 0;
}
//Map comparator function, just for information
bool operator<(const SLNetKey& lhs, const SLNetKey& rhs)
{
if (lhs.m_usrid != rhs.m_usrid)
return lhs.m_usrid < rhs.m_usrid;
if (lhs.m_portfolio != rhs.m_portfolio)
return lhs.m_portfolio < rhs.m_portfolio;
if (lhs.m_symbol != rhs.m_symbol)
return lhs.m_symbol < rhs.m_symbol;
return (int)lhs.m_enSide < (int)rhs.m_enSide;
}