Reverse C-style String? - C++

前端 未结 5 488
陌清茗
陌清茗 2021-01-01 06:10

I want to use pointers to reverse a char array in C++. I was wondering if there is anything that I should do differently? Am I doing this correctly? Is there a more efficie

5条回答
  •  鱼传尺愫
    2021-01-01 06:39

    You could use std::swap(*pStart, *pEnd) instead of open-coding swap.

    Heck, you could just use std::reverse(buffer, buffer + strlen(buffer)). But I suppose that wouldn't really be using pointers yourself, and given that requirement, it looks fine.

    Well, actually, a tiny nit: if length==0, then &string[length - 1] isn't pointing into the character array and is theoretically not a valid pointer.

提交回复
热议问题