Archived

This topic is now archived and is closed to further replies.

Joker2000

The "string" datatype...underrated?

Recommended Posts

Maybe it''s just me, but it seems like the string datatype is highly underrated/underused. I would like to know why. I mean, why would anyone want to use char name[50] over string name? Not only is it easier to manipulate string name (IMHO), but you don''t have to worry about overflowing any arrays because the size of a string dynamically changes, which I think would be the most important. However, despite the reasons I believe string is better, time and time (and time) again I see programmers, good programmers at that, using char arrays. Not only that, but it seems most compilers don''t even have support for the string datatype. They might let you use it, but they don''t provide any technical support for it. They appear to be saying, "Hey, you can use it all you want, but if you have trouble with it, don''t come to us!" That is just with Borland C++Builder 3.0 (the compiler I use). I haven''t looked at later versions, so I''m not sure if string has full support yet or not. Maybe this is the reason a lot of good programmers use char arrays. They have programmed with them for so long and gotten so good at using them, they don''t feel like messing around with a whole new datatype that does the same thing. I''m just curious as to why it hasn''t "caught on," if that''s the right word. I would have thought that string would be a blessing to the entire programming community. Also, for any of you out there who do use string extensively, do you know where there is a list of all the functions available for dealing with strings? For example:
string name;

name.length()
name.append()
name.insert()

// etc. etc.
 
Anyway, that''s all I have to say. Please reply if you can answer any of my questions, or if you have any comments on strings and/or char arrays. I''m anxious to hear what all of you have to say. --- Joker2000 Stevie Ray Vaughan - The Legend

Share this post


Link to post
Share on other sites
I can only guess that the reason most people don''t use it is that very few introductory C++ texts document it (probably because of their close ties to the STL, and because learning how to use char* strings can be greatly beneficial in learning how to deal with arrays and dynamically allocated memory), so they just aren''t familiar with it. I personally use them extensively.

For a list of string''s members, I''d suggest MSDN. C++: The Complete Reference, Third Edition covers them well too.

Share this post


Link to post
Share on other sites
1) What bug is that?
2) Anyone know how to do a case insensitive compare with two strings efficiently? in C I would have used stricmp. I suppose I _could_ d something pile (psudo)

stricmp(a.c_str(),b.c_str(),a.size());

But is that the best way?

Share this post


Link to post
Share on other sites
IMHO the String class is OVERRATED. I don't see anything wrong with the "char *" strings... Indeed, what could be simpler??? It's kind of silly to put a CLASS where there is already support for strings as well as <string.h>.

But I'm an ANSI C fanatic anyway...


Edited by - VGASteve on July 18, 2000 7:51:51 PM

Share this post


Link to post
Share on other sites
gimp:

the bug is that when the user types in a string that has whitespace they heave to hit enter twice. also it seems to cause overflows. Microsoft posted a fix on MSDN, but it still messes up sometimes.

Share this post


Link to post
Share on other sites
dj9781:

Needing to press enter twice doesn''t have anything to do with std::string, it''s the way the streams work in c++. You can either disable skipping of white space, or use getline e.g.


string str;
geline(cout, str);


quote:

I don''t see anything wrong with the "char *" strings... Indeed, what could be simpler???



You need to now the size of a char array before you use it. Your either going to waste memory by allocating to much, or not have enough space to store something so you need to miss off the end . If your not careful it''s easy to write passed the end of a char array. Contrary to what many C programs seem to think, the maxim length of a line isn''t 80.

If the string your using is reference counted, it could end up a lot faster than using C strings. If it''s not reference counted it wont be slower, but it will be safer.

Share this post


Link to post
Share on other sites
VGASteve:


#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void main()
{
string hello_world;
hello_world = string("hello ") + string("world");

fstream file("example.txt", ios::out);

cout << hello_world << endl;
file << hello_world << endl;
}



Yeah, I know it''s a lot harder to use the overloaded operators.


gimp:

I think the reason is that a lot of programmers who use C++ simply don''t know much about STL and/or templates. I almost always use the string class in my code, especially because it makes reading/writing to disk very easy.




- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
I think the reason it''s difficult to use the string class is because it''s hard to write to it in a stream. Yes, there is the sstream library, but I think it''s about the newest part of C++ and some compilers (gcc) don''t have it. So, the old standby of sprintf (char*,...) does a much better job. Also, having to unlock the memory once you print the sstream is just a major bug waiting to happen.

Share this post


Link to post
Share on other sites
gimp:

the best way to make a case insensitive string is to use basic_string with custom char_traits - ie derive from char_traits, make the ''compare'' func case insensitive then typedef a case insensitive string like this:

typedef basic_string string_nocase;

(but use better names!)


but if its just a comparison you''re after (not a full blown case insensitive string class), the stricmp method you used is fine.

Share this post


Link to post
Share on other sites