On Mon, Oct 18, 2010 at 9:24 PM, Rao, Anant <Anant.Rao@ironmountain.com> wrote:

Hi,

 

I need to iterate thru strings (For various reasons, I can’t use string or vector<char> containers).

I found that boost::range exactly fits the bill for me. I get all the convenience of an iterator (from as_literal.hpp).

 

I have a piece of code

char*      char_s = "I am fine";

char *cp;

std::size_t sz = strlen(char_s);

const char* str_end1 = str_begin( char_s ) + sz;

 

for (cp = str_begin(char_s); cp != str_end(char_s); ++cp)

    cout << *cp;

 

I have couple of questions:

 

1.       In the loop, boost::range seems to be calling strlen every iteration. This is an overhead. I was wondering if there’s a way I can pass in the length myself so that boost::range would use it and avoid calling strlen().

2.       If it’s not possible, is there a way for me to use iterators (boost or non-boost) over char strings?



I'm not entirely sure I understand your question, but does this meet your needs...

std::for_each( boost::begin(char_s), boost::end(char_s), your_functor );

or even

BOOST_FOREACH( char c, char_s )
{
    cout << c;
}

HTH

- Rob.