1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> myVec {1, 5, 5, 4, 3, 1, 2, 2, 2, 2, 6, 1, 3, 3, 5, 5, 5, 5 };
size_t run{};//to keep the run score
auto holding_itr = myVec.begin();//to keep the position score
auto running_itr = std::adjacent_find(myVec.begin(), myVec.end());
//http://en.cppreference.com/w/cpp/algorithm/adjacent_find
if(running_itr != myVec.end())
{
while (running_itr != myVec.end())
{
auto leading_itr = running_itr + 1;//for checking the next element
size_t tempRun{1};
while (leading_itr != myVec.end() && (*running_itr == *leading_itr))
{
if(*running_itr == *leading_itr)
{
++tempRun;
++leading_itr;
}
}
if (tempRun >= run )//if new max run found update as below
{
run = tempRun;
holding_itr = running_itr;
}
running_itr = std::adjacent_find(leading_itr, myVec.end());//now find the next run ...
}
}
std::cout << std::distance(myVec.begin(), holding_itr);
}
|