Home > Enterprise >  Convert borrowed_iterator<reverse_view<T>> to borrowed_iterator<T>
Convert borrowed_iterator<reverse_view<T>> to borrowed_iterator<T>

Time:12-26

I have buffer: char line[1024] which contains a line read from a file.

I want to find the last new-line (\n) in it, then replace all , with (space) before it.

The code I came up with:

const auto end = rng::find(line | rng::views::reverse, '\n'); // Find last occurrence of `\n`
rng::replace(line, end, ',', ' '); // Replace all `,` with ` ` before it

But it doesn't work because the type of line is a simple pointer, while end is a borrowed_iterator<reverse_view<T>>.

CodePudding user response:

while end is a borrowed_iterator<reverse_view<T>>.

In fact, the type of end is just std::reverse_iterator<char*>, you can use base() to get the underlying base iterator:

rng::replace(line, end.base(), ',', ' ');

Demo.

  • Related