Convert reverse_iterator to iterator?

2

I'm writing an XML interpreter in C ++, when I find a bad XML formation I'd like to show the line and column of the error. I'm working with string iterators and created a function to format the error as follows:

string msgLocalizadaXML(string::const_iterator aItIni, string::const_iterator aItFim, string::const_iterator aItMsg)
{
   string linha = "Linha...: " + to_string(count(aItIni, aItMsg, '\n') + 1) + '\n';
   constexpr auto numCaracteres = 25;

   auto itQuebra = find(reverse_iterator<string::const_iterator> { aItMsg },
                        reverse_iterator<string::const_iterator> { aItIni },
                        '\n');                                 
   // Linha do erro
   string coluna = "Coluna..: " + to_string(distance(itQuebra, aItMsg) + 1) + '\n';

   string contexto { max(aItIni, aItMsg - numCaracteres), min(aItFim, aItMsg + numCaracteres) };
   replace(contexto.begin(), contexto.end(), '\n', ' ');
   contexto = "Contexto: " + contexto + "\n"
              "          " + string(static_cast<size_t>(distance(max(aItIni, aItMsg - numCaracteres), aItMsg)), '~') + '^';

   return linha + coluna + contexto;
}

The error returned GCC 4.8.1:

XMLDocumento.cpp:420:86: error: no matching function for call to 'distance(std::reverse_iterator<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >&, std::basic_string<char>::const_iterator&)'

The error is because the iterators for the distance function have to be of the same type, but as I look forward to it it returns me a reverse, how do I convert it to an iterator?

I tried to build a std :: string :: iterator with itQuebra but it did not.

    
asked by anonymous 08.01.2015 / 23:13

1 answer

1

As specified in the question comments, resolution occurred using itQuebra.base() .

So, the code was changed from:

string coluna = "Coluna..: " + to_string(distance(itQuebra, aItMsg) + 1) + '\n';

To:

string coluna = "Coluna..: " + to_string(distance(itQuebra.base(), aItMsg) + 1) + '\n';
    
07.02.2015 / 01:09