Here's a handy fact about the C++ Standard Template Library that I didn't know: a char* (or any other pointer) is a valid iterator. This is because it is all templates, and iterators are anything that supports increment (++), and indirection (*); and whose elements support things like equality (==) and assignment (=).

For example, to search one chunk of memory for another chunk:

#include <algorithm>

char * pBuf = /* start of buffer to search */
char * pBufEnd = /* just past end of buffer */
char * pFind = /* data to find */
char * pFindEnd = /* just past end of data */

char * pFound = std::search(pBuf, pBufEnd, pFind, pFindEnd);

Just be careful: if the data isn't found, search returns pBufEnd, not NULL!

Found this in the C++ forum at Experts Exchange.

tagged:   /   via: Google» react

Comments

Add a comment:

name
email
Ignore this:
not displayed and no spam.
Leave this empty:
www
not searched.
 
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.