Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ strings

Reply
Thread Tools

C++ strings

 
 
Marcel
Guest
Posts: n/a
 
      01-18-2006
I am C++ beginner.

When i start building very basic things like hello world programs i am very
attired by the <string> library so i put all text things in strings. Is that
good programmers practice or should i do something else?

Marcel


 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-18-2006

Marcel wrote:
> I am C++ beginner.
>
> When i start building very basic things like hello world programs i am very
> attired by the <string> library


I don't know what you mean by that.

so i put all text things in strings. Is that
> good programmers practice or should i do something else?


That is usually good practice. There are conditions when using char*
is better but it is usually while dealing with C++/C mixtures such as
when using win32, a C API.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2006
Marcel wrote:
> I am C++ beginner.
>
> When i start building very basic things like hello world programs i am very
> attired by the <string> library so i put all text things in strings. Is that
> good programmers practice or should i do something else?


Hard to judge from one sentence. Sounds like you're going over the top,
and using string literals is perfectly fine as long as you declare them
arrays of const char, like so

const char programmer[] = "Marcel";

No need to declare it 'std::string', in most cases.

V
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-18-2006
* Marcel:
> I am C++ beginner.
>
> When i start building very basic things like hello world programs i am very
> attired by the <string> library so i put all text things in strings. Is that
> good programmers practice or should i do something else?


It's generally good practice for beginner's code -- and also for
experienced...

That way you won't hurt yourself.

However, there are some situations where it's ungood, mostly in code
that's not likely to be written by a beginner. Here's an example: you
need a text constant defined in one implementation file, used by some
code in another implementation file. A raw character array constant is
then initialized as part of what the standard calls "static
initialization", which happens before any of your code is executed,
whereas a std::string is an object with a constructor that's executed to
do the initialization, and so it's part of the "dynamic initialization",
which in certain situations just might happen _after_ the code using
that constant is executed -- and bang.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Marcel
Guest
Posts: n/a
 
      01-18-2006

"Marcel" <(E-Mail Removed)> schreef in bericht
news:60da2$43ce5529$d969db07$(E-Mail Removed) bel.net...
>I am C++ beginner.
>
> When i start building very basic things like hello world programs i am
> very attired by the <string> library so i put all text things in strings.
> Is that good programmers practice or should i do something else?
>
> Marcel
>


Ok thanks a lot!!!

Marcel


 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-18-2006

Victor Bazarov wrote:
> Marcel wrote:
> > I am C++ beginner.
> >
> > When i start building very basic things like hello world programs i am very
> > attired by the <string> library so i put all text things in strings. Is that
> > good programmers practice or should i do something else?

>
> Hard to judge from one sentence. Sounds like you're going over the top,
> and using string literals is perfectly fine as long as you declare them
> arrays of const char, like so
>
> const char programmer[] = "Marcel";
>
> No need to declare it 'std::string', in most cases.


Surely there's no harm in declaring it std::string instead of const
char[].

Once someone has mastered the simplest Hello World program, they'll
move on to the next level. Very soon they will probably encounter
things like reading strings from the user (e.g. "enter your name"),
passing strings to functions, concatenating them, modifying them. A
beginner (and indeed anyone else unless they have a good reason) should
be using std::string for these.

If your only interest in learning C++ is to write a Hello World
program, there's nothing to choose between std::string and const
char[]. If you want to learn a bit more about C++ than that then you'll
benefit from initally using only one technique for representing strings
and the best technique is std::string.

Gavin Deane

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2006
Gavin Deane wrote:
> [..some basic things named..]
> If you want to learn a bit more about C++ than that then you'll
> benefit from initally using only one technique for representing strings
> and the best technique is std::string.


I do not believe in purposely limiting the beginners to using some
language features and not others without any explanation, and simply
labeling them "the best technique". But it probably is already clear.

V
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-18-2006

Victor Bazarov wrote:
> Gavin Deane wrote:
> > [..some basic things named..]
> > If you want to learn a bit more about C++ than that then you'll
> > benefit from initally using only one technique for representing strings
> > and the best technique is std::string.

>
> I do not believe in purposely limiting the beginners to using some
> language features and not others without any explanation, and simply
> labeling them "the best technique". But it probably is already clear.


When I said 'best' I meant that I believe, when a beginner is learning
to use strings in simple contexts like concatenating, passing to
functions, basic user input, they are better off learning to do that
with std::string than char arrays.

If they are going to use std::string throughout lessons 2 to 5 (the
simple concepts I've suggested above) then why start them on char
arrays in lesson 1 (Hello World)?

Teaching only std::string at that stage is not hiding other language
features for the sake of hiding them. It is staying consistent for the
sake of avoiding the risk of introducing some unnecesary confusion.

Gavin Deane

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments