0
std::string line;

This throws std::runtime_error what(): Memory exhausted:

regex_it =  boost::sregex_iterator(line.begin(), line.end(), re);

This works fine:

regex_it = boost::make_regex_iterator(line, re);

Does anyone know what is causing the difference in performance? The boost::regex lib is compiled on Linux in default non-recursive mode.

EDIT: Also tried

regex_it = boost::cregex_iterator(line.data(), line.data()+line.size(), re);

same problem.

Marty B
  • 243
  • 3
  • 10

1 Answers1

2

Try working with a regex_iterator<char const*> rather than a regex_iterator<std::string::const_iterator>. (Also, the way you're calling make_regex_iterator is unnecessarily verbose by a large measure.)

Assuming line is a std::string, try this:

regex_it = boost::make_regex_iterator(line.c_str(), re);

or this:

regex_it = boost::cregex_iterator(line.data(), line.data() + line.size(), re);
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • +1 _unnecessarily verbose by a large measure_ -- what a nice way to put it – sehe Jun 14 '11 at 22:15
  • I guess I was a bit confused on template function use =P, thanks for that. That essentially solved my issue in a different way since it was the verbosity that was bugging me. However the cregex approach does not work, still getting the out of memory error. – Marty B Jun 14 '11 at 23:21
  • I realized I oversimplified the issue which left out the detail which was causing the problem. The `line.begin()` and `line.end()` was actually `line.substr(0,pos).begin()` and `line.substr(0,pos).end()`. Since the substring calls make temporary variables, that is copies of the substring, the begin and end iterators don't refer to the same container. I'm going to award this answer since it was the only one. – Marty B Jun 15 '11 at 07:08
  • @Marty : It's not considered bad form to answer your own question. :-] – ildjarn Jun 15 '11 at 18:45