Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NeonStorm

Storing large amounts of strings

This topic is 5357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I want to store strings somewhere so that I can access them later on in my main program. Pre-defined strings if you will. Basically what I want are strings that have an index value. So I can have a print function where you can parse a number, then it will lookup that number in the strings and print the coresponding string for that number. It sounds like an array job to me, but I''m not entirely sure how to do it (code wise - C, C++ etc.), so would someone mind helping me with this? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Well, if you are using stl strings then just declare and array or them..

string items[1000]; // 1000 strings!

or if you are using old school char arrays then use a 2d array..

string items[(number of items)][max string length]

and when you want to refer to an individual string, just refer to it as items[0], items[1], ect...

Share this post


Link to post
Share on other sites
In addition to that, you may want to look into using a data structure such as a map (std::map, etc). Each entry in the map is associated with a key, which can be used for indexing.

Share this post


Link to post
Share on other sites
quote:
Original post by NeonStorm
Ok, I was trying to do this in C with chars.
It''s complicated because of the nature of C-style strings. If you''re using C++ at all, skip it. If not...

char ** MsgStrings;
// allocate number of strings:
MsgStrings = (char **)malloc( NUM_STRINGS * sizeof(char *) );
 
// allocate each individual string:
for( int n = 0; n < NUM_STRINGS; ++n )
MsgStrings[n] = (char *)malloc( STR_LEN + 1 );
 
 
// with this method, you have to be careful about cleanup:
for( int n = 0; n < NUM_STRINGS; ++n )
free( MsgStrings[n] );
free( MsgStrings );

The alternative is to make the number of strings fixed, which reduces one level of memory management:

char * MsgStrings[1000]; // 1000 strings
 
// allocate each string:
for( int n = 0; n < 1000; ++n )
MsgStrings[n] = (char *)malloc( STR_LEN + 1 );
 
// cleanup:
for( int n = 0; n < 1000; ++n )
free( MsgStrings[n] );

quote:
STL strings I assume is string.h?
No, <string> (no ".h"). If you''re using this, you might as well use std::vector, too:

#include <string>
#include <vector>
 
using namespace std;
 
vector<string> MsgString;
 
...
 
// in this case you don''t have to do any memory management at all.
MsgString.push_back("Error Number Zero");
MsgString.push_back("Error Number One");
MsgString.push_Back("Error Number Two");
 
...
cout << MsgString[2] << endl; // prints "Error Number Two"

If you want to associate arbitrary indexes with strings, use a std::map instead of a std::vector.

Share this post


Link to post
Share on other sites
If you really want them pre-defined, it's easy... make an array of strings. C-style:


char* stingset[] =
{
"string 1",
"string 2",
...
"string n"
};



That does all the allocation for you.

C++-style:


#include <string>

std::string stringset[] =
{
"string 1",
"string 2",
...
"string n"
};


EDIT - But keep in mind, this way only works if they're static... use Oluseyi's method if you want dynamic strings.

[edited by - Chozo on April 15, 2004 1:16:13 AM]

[edited by - Chozo on April 15, 2004 1:17:35 AM]

Share this post


Link to post
Share on other sites
Thanks for all the replies.

Chozo, that''s what I originally had. I had two arrays with a few strings in them. I don''t know how to create a function to look up an element in a certain array, though. Here''s what I''ve got...

#include <stdio.h>

char *MSG_ACTION[] =
{
"test1",
"test2",
"test3"
};

char *MSG_STATUS[] =
{
"1test",
"2test",
"3test"
};

void printString(int, int);

int main()
{
int i, j;
print("Choose an array:\t");
scanf("%d", &i);
print("Chose an array element:\t");
scanf("%d", &j);
printString(i, j);
return 0;
}

// array determines which array we access
// element determines which array element (string) to print
void printString(int array, int element)
{
// what code do I put in here, any hints?
}

Share this post


Link to post
Share on other sites
Would this accomplish what you want? (Im a little rusty on the old C printf functions so be easy on me )


void printString(int array, int element)
{
/* Choose your own method for determining
what integral value will access what array */

if(array == 0) /* user wants msg action */
{
printf("%s", MSG_ACTION[element]);
}
else if(array == 1) /* user wants msg status */
{
printf("%s", MSG_STATUS[element]);
}
else
{
/* user entered an incorrect value */
return;
}
}


edit: used C++ style comments

[edited by - _Twiggie_ on April 15, 2004 2:45:12 AM]

Share this post


Link to post
Share on other sites
Yep, that was it Twiggie (though I decided to use a switch/case statement to make it a bit easier).

Can''t believe how simple it was, thanks a bunch!!

Share this post


Link to post
Share on other sites
You could also use a multi-dimensional array:


enum ArrayType
{
ActionArray,
StatusArray
};

char* arrays[][] =
{
{
"string 1",
"string 2",
...
"string n"
},
{
"string 1",
"string 2",
...
"string n"
}
}

print_string(ArrayType array, int element)
{
printf("%s", arrays[array][element];
}


Much faster, much cleaner. But it relies very heavily on your arrays being static.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!