| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Kevin Buffardi" |
| Date: |
19 Oct 2003 11:31:07 PM |
| Object: |
string literals |
To those experts in standard C/C++...
In past I've been quick to use the string.h library for convenience, but now
I'm working on a project where I'm trying to maximize portability so I want
to conform to c++ standards as much as I can. Can someone point me in the
right direction for using character arrays (I assume is the most convenient
way) specifically with assigning values and passing/returning from
functions.
I appreciate it. It's been a while since I've tried manipulating strings
without string.h and the only C++ book I have with me is specific to Borland
compilers and not standard C++. Thanks.
--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: string literals |
19 Oct 2003 11:47:36 PM |
|
|
"Kevin Buffardi" <kbuff1xw@mwc.edu> wrote in message
news:bmvo37$pbu6d$1@ID-80902.news.uni-berlin.de...
To those experts in standard C/C++...
In past I've been quick to use the string.h library for convenience, but
now
I'm working on a project where I'm trying to maximize portability so I
want
to conform to c++ standards as much as I can.
Can someone point me in the
right direction for using character arrays (I assume is the most
convenient
way)
IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.
specifically with assigning values and passing/returning from
functions.
The following (contrived) example demonstrates
passing strings (by reference) to a function,
and returning a string from a function:
#include <iostream>
#include <string>
std::string func(const std::string& arg1, const std::string& arg2)
{
return arg1 + ' ' + arg2;
}
int main()
{
std::string s1("Hello");
std::string s2("world");
std::cout << func(s1, s2) << '\n'; /* prints "Hello world" */
return 0;
}
I appreciate it. It's been a while since I've tried manipulating strings
without string.h and the only C++ book I have with me is specific to
Borland
compilers and not standard C++.
http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c__.htm
http://www.accu.org/bookreviews/public/reviews/0sb/advanced_c__.htm
http://www.parashift.com/c++-faq-lite/
http://www.contrib.andrew.cmu.edu/~ajo/faqs/acllc-c++.html#q6.3
-Mike
.
|
|
|
| User: "Kevin Buffardi" |
|
| Title: Re: string literals |
20 Oct 2003 12:59:31 AM |
|
|
IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.
That's standard? I had been using the string type, but I was under the
impression that it was not standard because the g++ compiler I was using
threw a fit when I tride to #include<string> .
--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
.
|
|
|
| User: "Gregg" |
|
| Title: Re: string literals |
20 Oct 2003 01:23:09 AM |
|
|
"Kevin Buffardi" <kbuff1xw@mwc.edu> wrote in
news:bmvt8u$rcvus$1@ID-80902.news.uni-berlin.de:
IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.
That's standard? I had been using the string type, but I was under
the impression that it was not standard because the g++ compiler I was
using threw a fit when I tride to #include<string> .
Yes, it's standard. You would know that if you had current reference
material. Set aside most C++ books written before 1998. Get a copy of
- The C++ Programming Language, 3rd edition, Stroustrup.
- The C++ Standard Library: A Tutorial and Reference, Nicolai Josuttis.
I highly recommend the latter if you want to learn about the standard
library, of which std::string is a part. In addition, if you want an
introduction to C++ for someone with programming experience, get a copy
of
- Accelerated C++, Andrew Koenig, Barbara Moo.
If your compiler does not accept
#include <string>
then it is either old (in the case of gcc, pre-3.0), not installed
correctly, or not being invoked correctly. Do
gcc --version
to see what version you have.
Gregg
.
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: string literals |
20 Oct 2003 04:50:08 AM |
|
|
Gregg wrote:
If your compiler does not accept
#include <string>
then it is either old (in the case of gcc, pre-3.0),
Actually, even gcc 2.91.66 (the oldest one I have - from early '99)
already has that header.
.
|
|
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: string literals |
20 Oct 2003 09:16:39 AM |
|
|
"Kevin Buffardi" <kbuff1xw@mwc.edu> wrote in message news:bmvt8u$rcvus$1@ID-80902.news.uni-berlin.de...
IMO the more 'convenient' way to work with strings is not
with arrays at all, but with the std::string type.
That's standard? I had been using the string type, but I was under the
impression that it was not standard because the g++ compiler I was using
threw a fit when I tride to #include<string> .
What kind of fit? Did you remember it's in namespace std? I use string with
G++ all the time.
.
|
|
|
| User: "Kevin Buffardi" |
|
| Title: Re: string literals |
22 Oct 2003 01:08:41 AM |
|
|
Did you remember it's in namespace std?
Ding ding ding.... we have a winner. Guessing it's been 3 years since I've
used string on g++ and completely forgot that it required that one little
line. Silly me.
--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
.
|
|
|
|
|
|
|
| User: "David White" |
|
| Title: Re: string literals |
19 Oct 2003 11:56:11 PM |
|
|
Kevin Buffardi <kbuff1xw@mwc.edu> wrote in message
news:bmvo37$pbu6d$1@ID-80902.news.uni-berlin.de...
To those experts in standard C/C++...
Standard C++ is all that matters here. C and C++ are different languages and
there's no such language as C/C++.
In past I've been quick to use the string.h library for convenience, but
now
I'm working on a project where I'm trying to maximize portability so I
want
to conform to c++ standards as much as I can. Can someone point me in the
right direction for using character arrays (I assume is the most
convenient
way) specifically with assigning values and passing/returning from
functions.
Surmising from your subject line, do you mean string literals only, or other
character arrays as well? String literals are pretty limited, since the
strings' contents have to known at compile time and can't be modified. Other
character arrays are definitely not the most convenient way of assigning
values and returning strings from functions.
I suggest that you use std::string objects (use the <string> header file)
for assigning string contents and returning strings from functions. For
passing to functions that don't modify the string contents, you can also use
std::strings, but const char* is often sufficient.
DW
.
|
|
|
| User: "Kevin Buffardi" |
|
| Title: Re: string literals |
20 Oct 2003 01:08:36 AM |
|
|
Standard C++ is all that matters here. C and C++ are different languages
and
there's no such language as C/C++.
Yes I know "C/C++" isn't a language, but I didn't know if there was a C
standard for string manipulation that was brought over to C++. In fact, my
code should also compile on a C-compiler, so either would work... hence my
original "C/C++" notation.
Surmising from your subject line, do you mean string literals only, or
other
character arrays as well? String literals are pretty limited, since the
strings' contents have to known at compile time and can't be modified.
You're right, the subject line is deceiving. I need to manipulate the
strings during run-time. I'm looking for a _portable_ solution that should
work with any C++ compiler on any platform. Given choices, I'd go for the
most convenient of them if they all satisfy the portability requirement.
However, if I was wrong and <string> is standard, then I'll just stay with
it, as it does what I need it to do and I've been using it for years now so
I'd consider it convenient.
--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson
.
|
|
|
| User: "Andrew Koenig" |
|
| Title: Re: string literals |
20 Oct 2003 09:23:20 AM |
|
|
However, if I was wrong and <string> is standard, then I'll just stay with
it, as it does what I need it to do and I've been using it for years now
so
I'd consider it convenient.
<string> is standard. Note, however, that <string.h> is something
completely different--it's part of the C standard library, and declares
functions such as strcpy.
.
|
|
|
|
| User: "Peter van Merkerk" |
|
| Title: Re: string literals |
20 Oct 2003 02:37:32 AM |
|
|
Standard C++ is all that matters here. C and C++ are different
languages
and
there's no such language as C/C++.
Yes I know "C/C++" isn't a language, but I didn't know if there was a
C
standard for string manipulation that was brought over to C++. In
fact, my
code should also compile on a C-compiler, so either would work...
hence my
original "C/C++" notation.
If the code should also compile on a C compiler you will have to do
without to all the benefits of C++, including the std::string class. The
vast majority of C code compiles also on C++ compilers (unless C99
extensions are used), including string manipulation. If the code really
should compile on a C compiler, it is better to start with a C compiler
first, and test later if it also compiles on a C++ compiler.
You're right, the subject line is deceiving. I need to manipulate the
strings during run-time. I'm looking for a _portable_ solution that
should
work with any C++ compiler on any platform. Given choices, I'd go for
the
most convenient of them if they all satisfy the portability
requirement.
If you are willing to say goodbye to C compatibility, std::string meets
your requirements perfectly.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
.
|
|
|
|
| User: "David White" |
|
| Title: Re: string literals |
20 Oct 2003 01:28:42 AM |
|
|
Kevin Buffardi <kbuff1xw@mwc.edu> wrote in message
news:bmvtq0$r95es$1@ID-80902.news.uni-berlin.de...
However, if I was wrong and <string> is standard, then I'll just stay with
it, as it does what I need it to do and I've been using it for years now
so
I'd consider it convenient.
Yep, the std::string type is definitely standard and portable.
DW
.
|
|
|
|
|
|

|
Related Articles |
|
|