4

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>>.

cigien
  • 57,834
  • 11
  • 73
  • 112
Pirulax
  • 80
  • 1
  • 7

1 Answers1

5

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.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • Okay.. I have no clue. I remember trying this, and it didn't work, now it does... Well, issue solved I guess. Thank you. Edit: Maybe IntelliSense was trolling me.. – Pirulax Dec 25 '21 at 19:00