Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 4th, 2006, 2:26 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile 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.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Oct 4th, 2006, 2:38 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Oct 4th, 2006, 8:23 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thanks Narue,
no wonder the book I looked at seems to almost ignore the subject of string arrays. On to the vectors I go!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Oct 5th, 2006, 12:22 AM   #4
climbnorth
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 climbnorth is on a distinguished road
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);
climbnorth is offline   Reply With Quote
Old Oct 5th, 2006, 6:09 PM   #5
PLJack
Newbie
 
PLJack's Avatar
 
Join Date: Oct 2006
Posts: 1
Rep Power: 0 PLJack is on a distinguished road
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
PLJack is offline   Reply With Quote
Old Oct 5th, 2006, 6:22 PM   #6
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Oct 5th, 2006, 6:26 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 5th, 2006, 7:19 PM   #8
climbnorth
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 climbnorth is on a distinguished road
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);
}
climbnorth is offline   Reply With Quote
Old Oct 5th, 2006, 7:21 PM   #9
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Oct 5th, 2006, 7:42 PM   #10
climbnorth
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 climbnorth is on a distinguished road
Narue-

I don't understand, can you elaborate? I don't understand what you mean by simulation of an array.
climbnorth is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c strings in an array problem monkijunki2 C 6 Nov 15th, 2005 11:42 AM
How to make an array of pointers-to-char point to strings aznluvsmc C 38 Sep 21st, 2005 8:45 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC