Re: I'd like to use STL algorithms
kanze ha scritto:
His iteration actually involves the numerical value of the
index. As such, any conversion to the STL is likely to be
obfuscation, or at least more work, requiring that he maintain
the index manually anyway. In general, when the numerical value
of the index is significant, the STL (nor any other library
based on iterators) is not the answer.
I believe it should be easy to implement an iterator adapter that
produces pairs (index, it). Basically it might be similar to
boost::counting_iterator holding two incrementables instead of one.
Then you could use something like:
std::transform(
make_double_counting_iterator(tokens.begin(), 0), // 0 is start index
make_double_counting_iterator(tokens.end(), -1), // -1 is a dummy
back_inserter(val),
convert_adapter());
where convert_adapter is
struct convert_adapter
{
template <class It, class Diff>
void convert(const std::pair<It, Diff>& p)
{ convert(p.second, *p.first); }
};
Apart from the necessity of the adapter, I don't see this code so
obfuscated. How would you rate this design? double_counting_iterator is
not tied to this problem domain and could also be useful as a generic tool.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]