2

I can't seem to be able to find an easy simple solution, how to get bidirectional iterator from char *buffer with defined bufferSize. I don't want to copy the buffer to std::string (too expensive), I just want something like std_whatever::buffer_wrapper myWrappedBuffer(myBuffer, mySize); and then use myWrappedBuffer.begin() and myWrappedBuffer.end() in <algorithm> functions. What is the simplest way to do that? I really don't want to implement the iterator myself. I know there is boost::string_ref and maybe std::string_view, but I don't want to use boost just for this and can't use c++17.

tach
  • 663
  • 12
  • 19

1 Answers1

8
char *b=buffer;

This is your beginning iterator.

char *e=buffer+buffersize;

This is your ending iterator.

Plain, garden variety pointers meet all requirements of not just bi-directional, but random access iterators, and can generally be used anywhere any kind of an iterator is required, and they'll work just like one. All (well-written) iterator-based templates will work with pointers just fine.

You can even feed them to std::iterator_traits, and get meaningful results. Try it yourself and see what happens.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148