void CFormDumper::ReverseSeq2(int nStart, int nEnd, vect_dumperVehi& vVehiInfo)
{
int nSize = 0;
nSize = m_vectVehiLeft.size();
if (0 == nSize)
{
return;
}
if (nStart < 0)
{
nStart = 0;
}
if (nStart >= nSize)
{
nStart = nSize;
}
if (nEnd >= nSize)
{
nEnd = nSize;
}
/*先将旧的数组清空,并备份当前车辆信息数组*/
m_vectOldVehiLeft.clear();
m_vectOldVehiLeft.assign(m_vectVehiLeft.begin(), m_vectVehiLeft.end());
int nIndex = 1;
vect_dumperVehi::iterator itr = m_vectVehiLeft.begin();
vect_dumperVehi::iterator itr_first = itr;
vect_dumperVehi::iterator itr_last = itr;
vect_dumperVehi::iterator itr_second = itr;
STRUCT_DUMPERSLIP_VEHI stru_vehi;
/*判断反序的车数是奇数还是偶数*/
BOOL bOdd = TRUE;/*初始为奇数*/
int nNum = 0; /*标记实际有奇数辆还是偶数辆*/
BOOL bTest = TRUE;
nNum = (nEnd - nStart + 1)%2;
if (nNum == 1)
{
bTest = TRUE; /*奇数*/
}
else
{
bTest = FALSE; /*偶数*/
}
/*对于选择奇数个车辆时*/
if (bTest)
{
itr_first = itr + nStart - 1;
nStart = nStart + 1; /*将起点后移一位,从偶数个车辆开始*/
/*遍历*/
while (itr != m_vectVehiLeft.end())
{
if (nIndex == nStart) /*起点*/
{
//itr_first = itr;
/*先处理第一个元素*/
stru_vehi = *itr;
vVehiInfo.push_back(stru_vehi);
/*下一个元素奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex > nStart && nIndex <= nEnd)
{
stru_vehi = *itr;
if (nIndex == nStart+1) /*第2个元素*/
{
vVehiInfo.push_back(stru_vehi);/*放到队列最后*/
}
else
{
if (bOdd)/*如果为奇数,插入到最后一个元素的前面*/
{
vVehiInfo.insert(vVehiInfo.begin(), stru_vehi);
}
else
{
vVehiInfo.insert(vVehiInfo.begin()+1, stru_vehi);
}
}
/*奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex == nEnd) /*终点*/
{
itr++;/*指针必须再向后移动一位,删除的时候,才能将itr_first与itr_last区间的值删掉*/
itr_last = itr;
break;
}
nIndex++;
itr++;
}
stru_vehi = *itr_first;
vVehiInfo.insert(vVehiInfo.end(), stru_vehi); /*将第一个元素插入到最尾部;也可用push_back()*/
// vVehiInfo.push_back(stru_vehi);
}
else /*对于选择偶数个车辆时*/
{
/*遍历*/
while (itr != m_vectVehiLeft.end())
{
int nSizeTemp = 0;
if (nIndex == nStart) /*起点*/
{
itr_first = itr;
/*先处理第一个元素*/
stru_vehi = *itr;
vVehiInfo.push_back(stru_vehi);
/*下一个元素奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex > nStart && nIndex <= nEnd)
{
stru_vehi = *itr;
if (nIndex == nStart+1) /*第2个元素*/
{
vVehiInfo.push_back(stru_vehi);/*放到队列最后*/
}
else
{
if (bOdd)/*如果为奇数,插入到最后一个元素的前面*/
{
vVehiInfo.insert(vVehiInfo.begin(), stru_vehi);
}
else
{
vVehiInfo.insert(vVehiInfo.begin()+1, stru_vehi);
}
}
/*奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex == nEnd) /*终点*/
{
itr++;/*指针必须再向后移动一位,删除的时候,才能将itr_first与itr_last区间的值删掉*/
itr_last = itr;
break;
}
nIndex++;
itr++;
}
}
/*将车辆信息数组去除反序的部分*/
m_vectVehiLeft.erase(itr_first, itr_last);
{
int nSize = 0;
nSize = m_vectVehiLeft.size();
if (0 == nSize)
{
return;
}
if (nStart < 0)
{
nStart = 0;
}
if (nStart >= nSize)
{
nStart = nSize;
}
if (nEnd >= nSize)
{
nEnd = nSize;
}
/*先将旧的数组清空,并备份当前车辆信息数组*/
m_vectOldVehiLeft.clear();
m_vectOldVehiLeft.assign(m_vectVehiLeft.begin(), m_vectVehiLeft.end());
int nIndex = 1;
vect_dumperVehi::iterator itr = m_vectVehiLeft.begin();
vect_dumperVehi::iterator itr_first = itr;
vect_dumperVehi::iterator itr_last = itr;
vect_dumperVehi::iterator itr_second = itr;
STRUCT_DUMPERSLIP_VEHI stru_vehi;
/*判断反序的车数是奇数还是偶数*/
BOOL bOdd = TRUE;/*初始为奇数*/
int nNum = 0; /*标记实际有奇数辆还是偶数辆*/
BOOL bTest = TRUE;
nNum = (nEnd - nStart + 1)%2;
if (nNum == 1)
{
bTest = TRUE; /*奇数*/
}
else
{
bTest = FALSE; /*偶数*/
}
/*对于选择奇数个车辆时*/
if (bTest)
{
itr_first = itr + nStart - 1;
nStart = nStart + 1; /*将起点后移一位,从偶数个车辆开始*/
/*遍历*/
while (itr != m_vectVehiLeft.end())
{
if (nIndex == nStart) /*起点*/
{
//itr_first = itr;
/*先处理第一个元素*/
stru_vehi = *itr;
vVehiInfo.push_back(stru_vehi);
/*下一个元素奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex > nStart && nIndex <= nEnd)
{
stru_vehi = *itr;
if (nIndex == nStart+1) /*第2个元素*/
{
vVehiInfo.push_back(stru_vehi);/*放到队列最后*/
}
else
{
if (bOdd)/*如果为奇数,插入到最后一个元素的前面*/
{
vVehiInfo.insert(vVehiInfo.begin(), stru_vehi);
}
else
{
vVehiInfo.insert(vVehiInfo.begin()+1, stru_vehi);
}
}
/*奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex == nEnd) /*终点*/
{
itr++;/*指针必须再向后移动一位,删除的时候,才能将itr_first与itr_last区间的值删掉*/
itr_last = itr;
break;
}
nIndex++;
itr++;
}
stru_vehi = *itr_first;
vVehiInfo.insert(vVehiInfo.end(), stru_vehi); /*将第一个元素插入到最尾部;也可用push_back()*/
// vVehiInfo.push_back(stru_vehi);
}
else /*对于选择偶数个车辆时*/
{
/*遍历*/
while (itr != m_vectVehiLeft.end())
{
int nSizeTemp = 0;
if (nIndex == nStart) /*起点*/
{
itr_first = itr;
/*先处理第一个元素*/
stru_vehi = *itr;
vVehiInfo.push_back(stru_vehi);
/*下一个元素奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex > nStart && nIndex <= nEnd)
{
stru_vehi = *itr;
if (nIndex == nStart+1) /*第2个元素*/
{
vVehiInfo.push_back(stru_vehi);/*放到队列最后*/
}
else
{
if (bOdd)/*如果为奇数,插入到最后一个元素的前面*/
{
vVehiInfo.insert(vVehiInfo.begin(), stru_vehi);
}
else
{
vVehiInfo.insert(vVehiInfo.begin()+1, stru_vehi);
}
}
/*奇偶性质改变*/
bOdd = !bOdd;
}
if (nIndex == nEnd) /*终点*/
{
itr++;/*指针必须再向后移动一位,删除的时候,才能将itr_first与itr_last区间的值删掉*/
itr_last = itr;
break;
}
nIndex++;
itr++;
}
}
/*将车辆信息数组去除反序的部分*/
m_vectVehiLeft.erase(itr_first, itr_last);
}
用来如何管理内存块使用示例。