Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Strings in C++
  
User name:
Password:
Reply:
 

UserPost
Dwedit
Posts: 77/92
Um... the string content itself uses heap memory. Making a 'new' string will just stick the pointer and length on the heap, which is pointless unless you really really want objects on the heap.
I never use 'new []' anyway, the vector class takes care of that for me.
ADnova
Posts: 9/9
Originally posted by Disch
I fail to see the difference between 'assigning' and 'copying'. I dislike how he says you can't assign a string to a char array, but says you can assign a string to a std::string object with the '=' operator. All the '=' operator is is like an overloaded version of strcpy -- why not list strcpy on the c-style string side?



Well, from a teaching point of view it makes sense. Telling students with only 1 C++ course under their belts that you can assign to C-strings is not a good idea. To most of them, assignment means use of the '=' operator, not calling strcpy. I think it's important while learning the details of a language to be as precise as possible.


Originally posted by Disch

Some other things were misleading... such as the concatenation example:

"strcpy(s, strcat(str1, str2)); str = str1 + str2;"

The char array examlpe will actually append str2 to str1, then copy str1 to s (modifying both str1 and s). Whereas the std::string side will only modify str.



Good point there, I might send him an email about that.
Dish
Posts: 483/596
I fail to see the difference between 'assigning' and 'copying'. I dislike how he says you can't assign a string to a char array, but says you can assign a string to a std::string object with the '=' operator. All the '=' operator is is like an overloaded version of strcpy -- why not list strcpy on the c-style string side?

Some other things were misleading... such as the concatenation example:

"strcpy(s, strcat(str1, str2)); str = str1 + str2;"

The char array examlpe will actually append str2 to str1, then copy str1 to s (modifying both str1 and s). Whereas the std::string side will only modify str.

Other than that it is pretty informative. One of the little benefits to working with char arrays is the power of having every character at your fingertips without having to work around class member functions. For instance I do the following a LOT:


char fullpath[MAX_PATH] = "C:\\Directory\\";
char* filename;

filename = fullpath + strlen(fullpath);

strcpy(filename,"file.txt");

printf( fullpath ); // will output "C:\Directory\file.txt"
printf( filename ); // will output "file.txt"


Using that I can maintain a buffer which always holds the full path to a file, but only modify 'filename' when I want to switch to another file in the same directory.


Stuff like that is definatly not as easy to do with std::string. However you can't really put that kind of thing in a simple comparison doc.
ADnova
Posts: 8/9
One of my profs wrote up a page comparing C and C++ strings:
http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html
Dish
Posts: 458/596
You can pass a static member function as a DlgProc. You can't use a non-static member function because they require an object to be called with. That's not a limitation of the class... but more because DlgProcs are expected to be pointers to global functions. If they were designed better, they'd allow you to give a pointer with the DlgProc so that it would be more OOP friendly... but alas it's too late for that.

Anyway, the reason I like classes is so I can avoid having global vars, and I don't have to do 'mystruct->varname' when referencing every variable in an object. Putting my entire program in a big class allows the whole program to have access to all the member vars no matter which .cpp file they're in ... without having to make the vars global (and do the whole 'extern' linking BS).

That's on top of the OOP benefits. I still use various classes I have sitting on my HD for various projects (like a sound output class, a PNG loader, OGG player, DInput wrapper, etc)
beneficii
Posts: 330/567
Classes are pretty useful, but they sometimes seem restrictive (like it seems you can't pass a pointer of a class function--say if you wanted to use a class function as a DlgProc), so I on some projects I may choose not to use them at all.

Plus, for me that may be "to each her own."
Dish
Posts: 457/596
I prefer to use char arrays as well. Then again I dislike a lot of the C++ additions (I only really use C++ for classes and a handful of other perks).

But meh, to each his own.
beneficii
Posts: 329/567
Originally posted by Disch
don't confuse this with Java. 'new' without a matching 'delete' will lead to memory leaks.


string myString("string literal");


^ is the proper way to use a class constructor in C++. Alternatively if you want to use heap memory:



string* myString = new string("string literal");

// do stuff with myString here

delete myString; // don't forget this line if using 'new' !!!!



Meh, I don't even know why people bother using the string class; quit being lazy and just use a char array/pointer! It's not that hard! If you want it to be of variable size, then just use malloc.
Dish
Posts: 456/596
don't confuse this with Java. 'new' without a matching 'delete' will lead to memory leaks.


string myString("string literal");


^ is the proper way to use a class constructor in C++. Alternatively if you want to use heap memory:



string* myString = new string("string literal");

// do stuff with myString here

delete myString; // don't forget this line if using 'new' !!!!
neotransotaku
Posts: 3644/4016
since string is a class... new String("string literal") wouldn't hurt either...
HyperLamer
Posts: 5989/8210
Have fun.

If you want to just put something into it: sprintf(string,"This is the number %d",42);. If you want to get input from a console: gets(string);, IIRC.
Ramsus
Posts: 151/162
The string header includes a getline template function that takes an istream reference and a string class reference. So:

using namespace std;

/* ... */
string mystring;
getline(cin, mystring);
cout << mystring << endl;

Takes one line from input, puts it in mystring, then outputs it.

Sandy53215
Posts: 790/948
When using strings in C++ (not Char arrays, but the string class from #include)
how is it possible to input data into them (or have the user input data into them).

a normal cin works unless their are spaces but im sure their is a much more appropriate way of doing it.

gets and cin.getline both fail with horrid error messages.
Acmlm's Board - I2 Archive - Programming - Strings in C++


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.011 seconds.