Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Array of Strings (http://www.programmingforums.org/showthread.php?t=11494)

Dietrich Oct 4th, 2006 2:26 PM

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

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    char str[] = "This is a sample string.";
    char *pch;
    char delimiter[] = " ,.";  // space comma period

    // create a string vector to hold the words
    vector<string> words;
   
    // get the first word/token
    pch = strtok(str, delimiter);
    while (pch != NULL)
    {
        // load the words vector
        words.push_back(pch);
        // next word
        pch = strtok(NULL, delimiter);
    }
   
    // test the words vector
    cout << "The string had " << words.size() << " words:\n";
    for (int k = 0; k < words.size(); k++)
    {
        cout << words[k] << endl; 
    }
           
    cin.get();  // wait for key press
    return EXIT_SUCCESS;  // optional
}

I am not familiar with vectors and wanted to use an array of strings instead. However, I am not quite sure how I would dimension such a beast to be as general as a vector seems to be.

Narue Oct 4th, 2006 2:38 PM

>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.

Dietrich Oct 4th, 2006 8:23 PM

Thanks Narue,
no wonder the book I looked at seems to almost ignore the subject of string arrays. On to the vectors I go!

climbnorth Oct 5th, 2006 12:22 AM

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;
char **blah;
// First, allocate memory for blah (this is an array of char * pointers!) 
char **blah = (char **)malloc(sizeof(char*) * numStrings);

for(i = 0; i < numStrings; i++)
{
  blah[i] = (char*)malloc(sizeof(char) * lengthofstring);
  memset(blah[i], 0, sizeof(char) * lengthofstring);
  memcpy(blah[i], sourcestring, sizeof(char) * lengthofstring);
}

// Now, free the strings in reverse order of the allocations.

for(i = 0; i < numStrings; i++)
{
  free(blah[i]);
}
free(blah);


PLJack Oct 5th, 2006 6:09 PM

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

Pizentios Oct 5th, 2006 6:22 PM

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!

DaWei Oct 5th, 2006 6:26 PM

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.

climbnorth Oct 5th, 2006 7:19 PM

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())
{
  szSize += strlen(new_stuff);
  realloc(myArray, zSize2);
  sprintf(myArray, "%s,%s", myArray, new_stuff);
}


Narue Oct 5th, 2006 7:21 PM

>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.

climbnorth Oct 5th, 2006 7:42 PM

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