@cauchy: a map of pairs would be slower and more complex than it needs to be. Dumping the results to stdout actually takes more lines than a simple solution:
// assuming compiler doesn't support initialization lists for std::vector:
int ai[] = { 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7 };
std::vector<int> values(ai, ai+(sizeof(ai)/sizeof(ai[0])));
std::vector<int> boundaries;
int prevValue = values[0] - 1;
for (size_t i = 0; i < values.size(); i++){
if (prevValue != values[i]){
boundaries.push_back(i);
prevValue = values[i];
}
}
// show lower boundaries
std::cout << "[" ;
for (size_t i = 0; i < boundaries.size(); i++){
std::cout << " " << boundaries[i];
}
std::cout << "]" << std::endl;
// show upper boundaries
std::cout << "[" ;
for (size_t i = 1; i < boundaries.size(); i++){
std::cout << " " << boundaries[i] - 1;
}
std::cout << " " << values.size() - 1;
std::cout << " ]" << std::endl;
Note that we only need the lower boundary index LB[i] for each value array element VA[n] because the upper boundary UB[i] for VA[n] will always be LB[i+1] – 1, except for UB[last] which is by definition the max valid index for VA.
solved how to find the maximum and minimum index of an element in a vector? [closed]