![]() |
Array of Strings
I am starting to learn C++ and found this code sample:
:
// split a string into its words and load them into a vector |
>I am not familiar with vectors and wanted to use an array of strings instead.
Not being familiar with vectors isn't an excuse. They're worth learning, especially when you consider the alternative. ;) >I am not quite sure how I would dimension such a beast to be as general as a vector seems to be. You would do it with great difficulty, and I wouldn't recommend it unless you're coming to C++ from a ten year bender in C. The vector class is designed to save us from the nightmare of managing the memory for a dynamic array. |
Thanks Narue,
no wonder the book I looked at seems to almost ignore the subject of string arrays. On to the vectors I go! |
if you want to go with teh array of arrays route using something like a char **, its not difficult but its a lot of work writing from scratch something that is not as flexible as a vector.
The below code allocates, then frees memory of something. Note that lengthofstring, numStrings, and sourcestring are not defined. :
int i; |
Hi guys. First post in PF.
Just wanted to chime in with my experience. I avoided learning vectors for quite some time. Regretted it after I took the time to learn how vectors work and what they can do for me. And don't fall for the performance / overhead argument. Super fast code does you nothing if your code crashes. Not that vectors are slow anyway. Stability with arrays is what forced me to learn vectors in the first place. Do yourself a favor and nail vectors now while you are still learning the basics of coding. PLJack |
one reason (read: the main reason) i use vectors over arrays is:
arrays cannot be dynamic (meaning you can't increase the size of the array after you declair it), where as vectors can be added to or subtracted from. Plus vectors have some cool functions that the arrays don't have. Off Topic: Welcome PLJack! |
While you're looking at vectors, look at deques, also. There are a few differences. Deques support contant-time removal or insertion at the beginning as well as at the end.
|
Pizentios:
On the contrary, you can reallocate memory using "realloc". This is useful if you want to constantly change the size of your chunk of memory. Below- making CSV of params: :
while(something()) |
>On the contrary, you can reallocate memory using "realloc".
But that's not an array, it's a simulation of an array. Get your facts straight before trying to correct someone. |
Narue-
I don't understand, can you elaborate? I don't understand what you mean by simulation of an array. |
| All times are GMT -5. The time now is 1:03 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC