regex_iterator with for_each question...
I thought that for_each() goes while (it != end). How does for_each() 'know' what end is here? It doesn't seem to be initialized to anything. Thanks much, Graham class regex_callback { int m_sum; public: regex_callback() : m_sum(0) {} template <typename T> void operator()(const T& what) { m_sum += boost::lexical_cast<int>(what[1].str().c_str()); } int sum() const { return m_sum; } }; int main() { boost::regex("(\\d+),?"); string s = "1,1,2,3,5,8,13,21"; boost::sregex_iterator it(s.begin(), s.end(), reg); // Is end initialized to anything here? boost::sregex_iterator end; regex_callback c; // How is end valid here? int sum = for_each(it, end, c).sum(); return 0; }
Graham Reitz wrote:
int main() { boost::regex("(\\d+),?"); string s = "1,1,2,3,5,8,13,21";
boost::sregex_iterator it(s.begin(), s.end(), reg); // Is end initialized to anything here? boost::sregex_iterator end;
regex_callback c;
// How is end valid here? int sum = for_each(it, end, c).sum();
return 0; }
Your code seems to work fine. ('reg' isn't defined, though :-) You want to know how to work? 'it' automagically turns into the value that equals to 'end' when 'it' reaches to 's.end()'. Thus everything works fine. You can look into <boost/regex/v4/regex_iterator.hpp> in detail. Regards, -- Shunsuke Sogame
participants (3)
-
Graham Reitz
-
John Maddock
-
shunsuke