vector<vector>排序

2023-08-24 12:59:17 浏览数 (1)

已知:每类奖励下面不止四种,是不确定数量(至少一种)。要求有可领取奖励的块上移到顶,如果有多个可领奖励块,它们的前后顺序和原来一样。这里每个标题栏和可领取块都是一张map里面的元素。如下图:

解:1.std::map<int, STRCARD>表按照标题分类转为一张std::vector<std::vector<STRCARD>>。(最后一行不可遗漏)

代码语言:javascript复制
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>>

代码语言:javascript复制
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]);
		}
	}

源码下载

http://download.csdn.net/detail/luoyikun/8896539

0 人点赞