473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

std::string::iterator use with char*

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
3 14343
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
> 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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
11
by: Sims | last post by:
Hi, I have a function that looks something like .... void Function( std::string &oldval ) { int iSome_size = xxx; // get some size char *tmp = NULL; tmp = new char;
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
5
by: Abubakar | last post by:
Hi, how do I convert std::string to char * ? regards, -Ab.
6
by: Hector Y. Martinez | last post by:
Well, here is my problem, I'm trying to convert an std::string variable to a char*, when I do std::string foo = "try something"; char* foo2 = foo.c_str(); an error ocurre, c_str() return a...
3
by: ruffiano | last post by:
Hello, I need your expertise to perform a conversion. I need to convert a std::string to a char. However, in the conversion, I need to get rid of the '/0' character. The initial string...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.