473,394 Members | 1,841 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,394 software developers and data experts.

C++ strings

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
Jan 18 '06 #1
7 1713

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.

Jan 18 '06 #2
* 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?
Jan 18 '06 #3
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
Jan 18 '06 #4

"Marcel" <so***************@nospam.com> schreef in bericht
news:60***************************@cache10.multika 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
Jan 18 '06 #5

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

Jan 18 '06 #6
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
Jan 18 '06 #7

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

Jan 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.