![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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 looked it up on the Intergnats! |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
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); |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2006
Posts: 1
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() |
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! |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#9 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
Narue-
I don't understand, can you elaborate? I don't understand what you mean by simulation of an array. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c strings in an array problem | monkijunki2 | C | 6 | Nov 15th, 2005 10:42 AM |
| How to make an array of pointers-to-char point to strings | aznluvsmc | C | 38 | Sep 21st, 2005 7:45 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |