By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

std::string::iterator use with char*

P: n/a
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-
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Brett Robichaud wrote:
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?
Jul 22 '05 #2

P: n/a
> 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

Jul 22 '05 #3

P: n/a

"Bart Blommerde" <ba**@ilse.net> wrote in message news:41***********************@diablo.nl.easynet.n et...
pBuf;
std::string::iterator start =


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.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.