std::string::iterator use with char*

B

Brett Robichaud

What is the correct way to allocate a string:iterator using a char*
buffer? In Visual C++ 7.1 the following works fine (where pBuf is a
char*):

std::string::iterator start = pBuf;

But under gcc 3.2.3 for RedHat linux it's not. I get an error like
this:

XmlParser.h:775: conversion from `char*' to non-scalar type
`__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >' requested

I assume I'm using some compiler dependent features of STL and I'd
like to know the correct generic way to do this.

Can anyone help?

-Brett-
 
C

Chris Jefferson

Brett said:
What is the correct way to allocate a string:iterator using a char*
buffer? In Visual C++ 7.1 the following works fine (where pBuf is a
char*):

std::string::iterator start = pBuf;

But under gcc 3.2.3 for RedHat linux it's not. I get an error like
this:

XmlParser.h:775: conversion from `char*' to non-scalar type
`__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >' requested

I assume I'm using some compiler dependent features of STL and I'd
like to know the correct generic way to do this.

Can anyone help?

The very short answer is that a std::string::iterator and char* don't
have to have any kind of sensible relation at all. If you want a
std::string::iterator, you can only take it from a std::string. If you
want to perform operations which require random-access iterators, then
you should be able to pass them char* pointers. Could you give a
specific example of what you are trying to do?
 
B

Bart Blommerde

What is the correct way to allocate a string:iterator using a char*
buffer? In Visual C++ 7.1 the following works fine (where pBuf is a
char*):

std::string::iterator start = pBuf;

Initializing an iterator with a character pointer is rather nasty, since
iterators are NOT pointers. I'd say the only correct way of doing this
is by first making it a string.

string s(pBuf);
std::string::iterator start = s.begin();

But you probably already thought of that...




regards,
Bart
 
R

Ron Natalie

Bart Blommerde said:
Initializing an iterator with a character pointer is rather nasty, since
iterators are NOT pointers. I'd say the only correct way of doing this
is by first making it a string.

string s(pBuf);
std::string::iterator start = s.begin();

But you probably already thought of that...

The other thing to observe that a char* pointing into a char array meets
the requirements of an iterator. For operations that take an arbitrary
iterator, a char* will work as a bidirectional iterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top