vector<vector>排序
原创
©著作权归作者所有:来自51CTO博客作者四夕立羽的原创作品,请联系作者获取转载授权,否则将追究法律责任
已知:每类奖励下面不止四种,是不确定数量(至少一种)。要求有可领取奖励的块上移到顶,如果有多个可领奖励块,它们的前后顺序和原来一样。这里每个标题栏和可领取块都是一张map里面的元素。如下图:
解:1.std::map<int, STRCARD>表按照标题分类转为一张std::vector<std::vector<STRCARD>>。(最后一行不可遗漏)
std::vector<STRCARD> vecCard;
vecCard.clear();
for (int i = 0; i < m_mapCard.size(); i++)
{
if (m_mapCard[i].bTitle == true && i!=0)
{
std::vector<STRCARD> vecCardTmp;
for (int i = 0; i < vecCard.size(); i++)
{
vecCardTmp.push_back(vecCard[i]);
}
m_vecCard.push_back(vecCardTmp);
vecCard.clear();
vecCard.push_back(m_mapCard[i]);
}
else
{
vecCard.push_back(m_mapCard[i]);
}
}
m_vecCard.push_back(vecCard);
2.对形成的std::vector<std::vector<STRCARD>>排序,最后形成排好序的std::vector<std::vector<STRCARD>>
std::vector<std::vector<STRCARD>> m_vecCardTmp;
for (int i = m_vecCard.size()-1; i >=0 ; i--)
{
if (true == getJudge(m_vecCard[i]))
{
m_vecCardTmp.insert(m_vecCardTmp.begin(),m_vecCard[i]);
}
else
{
if (m_vecCardTmp.size() <= 1)
m_vecCardTmp.insert(m_vecCardTmp.end(), m_vecCard[i]);
else
m_vecCardTmp.insert(m_vecCardTmp.end()-1, m_vecCard[i]);
}
}