-
August 25th, 2007, 09:01 AM
#1
char vs string
Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.
My query is, which one IS better? I know that I have always had alot of trouble with the char datatype just because it can have a subscript, and it can do without a subscript. Also, there dont seem to be predefined functions to work with the datatype char so it is just generally a pain in the butt even if you produce your own functions.
Now string on the other hand has a slew of functions already built to work with it such as .length(), AND its just all around easier to work with.
What do you guys think?
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
August 25th, 2007, 09:16 AM
#2
Re: char vs string
I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.
- Matt J Thomas
-
August 25th, 2007, 09:23 AM
#3
Re: char vs string
string is better than using plain chars yourself.
because u dont have to worry about allocations and such.
but string is still not a decent class (most of the STL classes arent in my opinion, but thats why they are called containers)
-
August 25th, 2007, 12:00 PM
#4
Re: char vs string
Originally Posted by RaleTheBlade
Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.
Have your instructor look at this document, written by the inventor of C++, Bjarne Stroustrup. He disagrees with your teacher, and gives reasons why.
http://www.research.att.com/~bs/new_learning.pdf
Also, char is only a single character. That is not a string. If your teacher means char arrays, then there are security issues that arise when handling char arrays, such as buffer overruns. Some C++ shops disallow any use of char arrays and functions such as strcpy(), strcat(), etc. because of these reasons.
Regards,
Paul McKenzie
-
August 25th, 2007, 12:02 PM
#5
Re: char vs string
Originally Posted by Matt J Thomas
I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.
You can still use strings. You have the c_str() member function, and if the string is writeable, you have std::vector<char>.
In this day and age of C++, rarely, if ever, do you need to use the new[]/delete[] way of creating a string or string buffer.
Regards,
Paul McKenzie
-
August 25th, 2007, 12:53 PM
#6
Re: char vs string
I find it amusing that a CS instructor would profess character arrays to be superior to string classes.
I would genuinely enjoy reading his reasoning if you would be so kind as to post it. In true Platonic spirit I would want to hear of a salient point that might be illuminating, even if I must admit I doubt there can be any.
There are a number of good string classes, and the STL string class may not be the best of them. I've used a portable, 'almost' drop in replacement of the MFC CString class (also loosely based upon the STL basic_string) for portable development with good effect. I suppose one of the reasonable arguments against string classes (though not compelling IMO) is the fact that there are several string classes. I suppose these days there may be agreement that the STL string class is the standard, but obviously developers using various frameworks have other relationships to consider (MFC's use of CString for example).
The const char array (and char array) is generally widely applicable, and perhaps that's what the professor is thinking.
Containment is more compelling as a reason, I think - favoring string classes.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
August 25th, 2007, 01:31 PM
#7
Re: char vs string
Originally Posted by Paul McKenzie
^^^^^^^^^^^^
Impressive Paul, thanks! I read that and got a better idea of just why strings are generally better.
Im not really sure why he thinks char's and char arrays are better. Personally I hate them, lol. And to answer your question JVene, I believe he said something about a string variable not having a null terminator at the end. And also something about the spaces between collective words in a string causing search issues or something. My mind kind of shut off as I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string.
OFF TOPIC EDIT:
On a side note, I just put honey in my coffee. Its pretty good actually
Last edited by RaleTheBlade; August 25th, 2007 at 01:39 PM.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
August 25th, 2007, 01:41 PM
#8
Re: char vs string
I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string
If he does not discard classes, you could write your own string class and use that.
-
August 25th, 2007, 01:53 PM
#9
Re: char vs string
Originally Posted by laserlight
If he does not discard classes, you could write your own string class and use that.
LOL, I was already thinking about that. I was actually working on a class with a function to compare char arrays to strings but then I was like "Why dont I just demand that I use string? Its me that paying for the education!"
And besides, if he demands that we use char... he's not really teaching us to use things the proper way, is he? Strings from what Ive always known in the past, are just better on many different levels.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
August 25th, 2007, 04:06 PM
#10
Re: char vs string
They are better on many levels.
The 'zero termination' notion is a red herring.
I have no idea what spaces have to do with it - sounds odd.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
August 25th, 2007, 04:08 PM
#11
Re: char vs string
the only reason i see fit to use dynamic char arrays and handling them yourself is to learn why string classes are better and how to make them yourself and how to learn how memory works. but if he is using this method to teach you C++ he is seriously wrong in doing so
-
August 25th, 2007, 04:17 PM
#12
Re: char vs string
Bravo Mitsukai!
Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"
A lesson more valuable than simply being told!
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
August 25th, 2007, 04:22 PM
#13
Re: char vs string
Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"
Student: "Yum! Tasty! Where did my hand go?"
-
August 25th, 2007, 04:27 PM
#14
Re: char vs string
Using a string is a much better choice than a char array. UNLESS, the only thing you are doing is storing text data, with possible appends.
If you are inserting, etc... Then a string type is better.
-
August 25th, 2007, 06:01 PM
#15
Re: char vs string
I knew string was better, I will continue to use it until he tells me to stop (or gives me an F) and then I will bring up the points you guys have told me. Thanks!
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|