64,651
社区成员
发帖
与我相关
我的任务
分享
std::vector<int>::iterator iterRes = resVector.begin();
std::vector<int> insert;
while(iterRes != resVector.end())
{
std::vector<int>::iterator iterTmp = iterRes;
iterRes++;
if(iterRes != resVector.end() && *iterRes - *iterTmp == 1)
{
insert.push_back(*iterTmp);
}
else if(iterRes != resVector.end() && *iterRes - *iterTmp != 1)
{
insert.push_back(*iterTmp);
allVectors.push_back(insert);
insert = std::vector<int>();
}
else if(iterRes == resVector.end())
{
insert.push_back(*iterTmp);
allVectors.push_back(insert);
}
}
#include "stdafx.h"
#include <list>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
std::list<std::vector<int> > allVectors;
std::vector<int> resVector;
// 测试数据,3组
for(int i = 100; i < 105; i++)
{
resVector.push_back(i);
}
for(int i = 120; i < 125; i++)
{
resVector.push_back(i);
}
for(int i = 150; i < 155; i++)
{
resVector.push_back(i);
}
std::vector<int>::iterator iterRes = resVector.begin();
std::vector<int> insert;
do
{
if(iterRes !=resVector.end())
{
std::vector<int>::iterator iterTmp = iterRes;
iterRes++;
if(iterRes != resVector.end() && *iterRes - *iterTmp == 1)
{
insert.push_back(*iterTmp);
}
else if(iterRes != resVector.end() && *iterRes - *iterTmp != 1)
{
insert.push_back(*iterTmp);
allVectors.push_back(insert);
insert = std::vector<int>();
}
else if(iterRes == resVector.end())
{
insert.push_back(*iterTmp);
allVectors.push_back(insert);
}
}
}while(iterRes != resVector.end());
// 循环结束,所有的分组均保存在allVectors中
return 0;
}