String with integers?

P

peter koch

Indeed, my mistake. Its been so long since I've had to worry about
low-level string manipulation that I didn't realize itoa was a
non-standard extension.

stringstream seems like the way to go then.

I believe that for convenience, there is no alternative to
stringstream. But I am surprised that anyone suggested atoi as the
primary function when we all (should) know that strtol is the C-way to
go.

/Peter
 
J

James Kanze

These days? Wikipedia. :)

That is NOT the place to look up a standard function. One of
the established standards or an established reference work would
be preferable, if you're interested in portability. Otherwise,
the documentation for your compiler.

Which, when carefully read, does say that 1) the function isn't
standard, and can't be used portably, and 2) presents a number
of different signatures which it has had in older systems, so
even if you've got it, you have to first find out which version
you've got.
There's even an awesome link to some ancient man pages there.

I've got a copy of the version 7 Unix manuals on my machine
somewhere, too. I don't use them to find out about the state
of things today. What's more significant in the Wikipedia
article is what's missing: any links to a modern specification.
 
J

James Kanze

The best thing you can do is use following code instead:
std::string str = "test";
char temp[100];
str += itoa(222, temp, 10);

Which beggers the question, since you then have to write itoa.
(Back in the old days, there was a function under Unix by this
name, but it's not part of the standard, and not supported by
most compilers or systems.)
 
P

peter koch

That is NOT the place to look up a standard function.  One of
the established standards or an established reference work would
be preferable, if you're interested in portability.  Otherwise,
the documentation for your compiler.
I never use wikipedia look up C++ questions, but I do use the internet
quite often. And I often use Wikipedia if there is other stuff I want
to check out.
Which, when carefully read, does say that 1) the function isn't
standard, and can't be used portably, and 2) presents a number
of different signatures which it has had in older systems, so
even if you've got it, you have to first find out which version
you've got.

And you did not have to read very carefully. I found the description
there quite accurate and easy to read.
I've got a copy of the version 7 Unix manuals on my machine
somewhere, too.   I don't use them to find out about the state
of things today.  What's more significant in the Wikipedia
article is what's missing: any links to a modern specification.

But they do link to opengroup.org for strtol, and the skim of that
documentation looked quite good. Just for the fun of it, it also
looked up std::vector amd I believe the documentation there also was
fine, even if I would not use it as a reference. For a tutorial it was
great although I found the illustrations to push_back and pop_back a
little confusing. The linkswere to places I did not know - and to sgi
which is not to good, but there were references to the C++ standard
which is good.
All in all wikipedia did well here.

/Peter
 
J

James Kanze

I never use wikipedia look up C++ questions, but I do use the
internet quite often. And I often use Wikipedia if there is
other stuff I want to check out.

I love the Wikipedia, and use it for a lot of things. But not
as a definitive reference work, for anything.

As for looking up the information on the Internet...
Regretfully, there are a lot of bad sites out there. Unless you
have some means of judging the quality or the authority of the
site, you have to be careful. (With some experience, of course,
you can often learn to recognize which sites are "authorities",
the Open Unix Standard for Unix, for example. Regretfully, I
don't think there is one for C++.)

And you did not have to read very carefully. I found the
description there quite accurate and easy to read.

Yes, I read it carefully. While there's certainly nothing false
in it, I found the introduction somewhat misleading:
"widespread", "often provided", etc. Still, it's a lot better
than a few others I've seen.
But they do link to opengroup.org for strtol, and the skim of
that documentation looked quite good.

Yes. My criticism here wasn't so much one of the Wikipedia
page, but of 1) it being cited as a reference for the function
(even though the page itself says that the function cannot be
used in portable code), and 2) the reference to "an awesome
link", which isn't relevant to the current question.
Just for the fun of it, it also looked up std::vector amd I
believe the documentation there also was fine, even if I would
not use it as a reference.

Because that's not its role. Especially not in such cases,
where accessible definite references (the original sources)
exist. I'm not criticizing Wikipedia, just the way it's being
used.
For a tutorial it was great although I found the illustrations
to push_back and pop_back a little confusing. The linkswere to
places I did not know - and to sgi which is not to good, but
there were references to the C++ standard which is good.
All in all wikipedia did well here.

It usually does. The only real problems are that it's not
always obvious when it doesn't, and that too many people are
trying to use it for something it isn't: an original source or a
definitive reference. It's probably not a good tutorial,
either---if you want to learn the STL, I'd recommend a book
(Matt Austern or Niko Josuttis), for example, rather than the
Wikipedia.
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,426
Latest member
MrMet

Latest Threads

Top