Is it possible to pass a raw pointer to a template function waiting for an iterator?

Is it possible to pass raw pointers to template functions waiting for iterators? Do I think correctly that an iterator is just a class that overrides the operators associated with the pointer (for example, *, ++, etc.), or do the iterators reveal some additional interface that the pointers do not have? In other words, are pointers "similar" to iterators?

Example:

I want to use boost::algorithm::knuth_morris_pratt_search ( here ).

My body (search string) and pattern (search string) are just bytes in memory - I have a pointer containing the start address and the length in bytes. For the sake of argument, let's say this is a c-style string.

According to the documentation, the knuth_morris_pratt_search function requires me to pass start and end iterators for both the case and the template.

The function I want to use:

 template <typename patIter, typename corpusIter> corpusIter knuth_morris_pratt_search ( corpusIter corpus_first, corpusIter corpus_last, patIter pat_first, patIter pat_last ); 

Can I do it?

 // Assume these are initialized: char* c; int cLength; char* p; int pLength; char* result = knuth_morris_pratt_search<char*, char*> (c, c + cLength, p, p + pLength); 
+4
source share
1 answer

I understand correctly that an iterator is just a class that overrides the operators associated with the pointer (e.g. *, ++, etc.),

You're right; pointers to data stored in arrays meet the requirements for random access iterators, that is, the most "complete" type of iterator, so you can use them essentially in any standard library algorithm.

I do not have a standard for a complete reference on random access iterators, but see, for example, here ; also here is a good diagram with various "types" of iterators.

+5
source

All Articles