Search code examples
c++stringpointers

Print text between pointers


I have a character range with pointers (pBegin and pEnd). I think of it as a string, but it is not \0 terminated. How can I print it to std::cout effectively?

  • Without creating a copy, like with std::string
  • Without a loop that prints each character

Do we have good solution? If not, what is the smoothest workaround?


Solution

  • You can use ostream::write, which takes pointer and length arguments:

    std::cout.write(pBegin, pEnd - pBegin);