If I have an iterator, I can get the equivalent reverse_iterator easily
enough.
vector<string>::const_iterator it = ...;
vector<string>::const_reverse_iterator rit(it);
What about if I have a reverse_iterator and want iterator? This doesn't work:
vector<string>::const_reverse_iterator it = ...;
vector<string>::const_iterator fit(it);
I guess I could use something like
fit = begin + std::distance(it, rend)
but there must be a clean way?
Any help appreciated,
Angus
ps I'm trying to clean-up yet another piece of nastiness that I wrote before
Lars breaks it ;-)
vector<string>::const_iterator
regexSearch(InfoMap const & theMap,
vector<string> const & keys,
string const & expr,
vector<string>::const_iterator start,
Direction dir)
{
boost::regex reg(STRCONV(expr));
for (vector<string>::const_iterator it = start;
// End condition is direction-dependent.
(dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
// increment is direction-dependent.
(dir == FORWARD) ? (++it) : (--it)) {
string data = (*it);
InfoMap::const_iterator info = theMap.find(*it);
if (info != theMap.end())
data += " " + info->second;
if (boost::regex_match(STRCONV(data), reg)) {
return it;
}
}
return keys.end();
}
I believe that this will be cleaner --- if I can get it working...
struct regexMatch
{
regexMatch(InfoMap const & m, boost::regex const & r)
: map_(m), regex_(r) {}
bool operator()(string const & key) {
string data = key;
InfoMap::const_iterator info = map_.find(key);
if (info != map_.end())
data += " " + info->second;
return boost::regex_match(STRCONV(data), regex_);
}
private:
InfoMap const & map_;
boost::regex const & regex_;
};
vector<string>::const_iterator
regexSearch(InfoMap const & theMap,
vector<string> const & keys,
string const & expr,
vector<string>::const_iterator start,
Direction dir)
{
boost::regex reg(STRCONV(expr));
regexMatch match_it(theMap, reg);
if (dir == FORWARD)
return std::find_if(start, keys.end(), match_it);
typedef vector<string>::const_iterator fwd_iterator;
typedef vector<string>::const_reverse_iterator rev_iterator;
rev_iterator rstart(start);
rev_iterator it = std::find_if(rstart, keys.rend(), match_it);
return fwd_iterator(it);
}