string iterator incompatible for reading eachline

452 Views Asked by At

I have an std::ostringstream. I would like to iterate for each line of this std::ostringstream.

I use boost::tokenizer :

std::ostringstream HtmlStream;
.............
typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer;
line_tokenizer tok(HtmlStream.str(), boost::char_separator<char>("\n\r"));

for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end(); i != end; ++i)
{
    std::string str = *i;
}

On the line

for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end(); i != end; 

I have an assert error with "string iterator incompatible". I have read about this error, on google and on StackOverflow too, but i have diffuclty to find my error.

Anyone could help me please ?

Thanks a lot,

Best regards,

Nixeus

1

There are 1 best solutions below

9
On BEST ANSWER

I like to make it non-copying for efficiency/error reporting:

See it Live on Coliru

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <iostream>
#include <vector>

int main()
{
    auto const& s = "hello\r\nworld";

    std::vector<boost::iterator_range<char const*>> lines;
    boost::split(lines, s, boost::is_any_of("\r\n"), boost::token_compress_on);

    for (auto const& range : lines)
    {
        std::cout << "at " << (range.begin() - s) << ": '" << range  << "'\n";
    };
}

Prints:

at 0: 'hello'
at 7: 'world'

This is more efficient than most of the alternatives shown. Of course, if you need more parsing capabilities, consider Boost Spirit:

See it Live on Coliru

#include <boost/spirit/include/qi.hpp>

int main()
{
    std::string const s = "hello\r\nworld";

    std::vector<std::string> lines;

    {
        using namespace boost::spirit::qi;
        auto f(std::begin(s)), 
             l(std::end(s));
        bool ok = parse(f, l, *(char_-eol) % eol, lines);
    }

    for (auto const& range : lines)
    {
        std::cout << "'" << range  << "'\n";
    };
}