[Please do not mail me a copy of your followup]
Suppose I have a vector of pairs of integers, e.g.
using pair_vec = std::vector<std::pair<int, int>>;
Where the elements of v are sorted by the first int in the pair:
{ {0,1}, {0,2}, {0,10}, {1,10}, {1,4}, {1,6}, {2,4}, {2,6}, {3,0} }
I need to do a certain amount of work once for each of the first
integers in the pair and a certain amount of work for each of the 2nd
integers in the pair, using some of the data computed for each of the
first integers.
Graphically, the outer loop is once for each of these chunks of the
sequence:
{ {0,1}, {0,2}, {0,10}, {1,10}, {1,4}, {1,6}, {2,4}, {2,6}, {3,0} }
+------------------+ +------------------+ +----------+ +---+
The inner loop is over each of the elements in the subsequence.
So I end up with a raw loop like this (not tested on a compiler):
void f(const pair_vec &v)
{
auto it = v.begin();
while (it != v.end())
{
auto state1 = do_work(it->first);
int sentinel = it->second;
for(; it != v.end() && it->second == sentinel; ++it)
{
more_work(state1, it);
}
}
}
What's an alternative for this raw loop using C++11 standard
algorithms that doesn't involve traversing the pair_vec elements more
than once?
(I think ranges would make it fairly easy, but I'm constrained to
using C++11 algorithms in this scenario.)
--
"The Direct3D Graphics Pipeline" free book <
http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <
http://terminals-wiki.org>
The Computer Graphics Museum <
http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <
http://legalizeadulthood.wordpress.com>