Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 08:15 PM
slack_justyb
Guest
 
Posts: n/a
Default Best way to initialize an array of strings

Hello,

I'm trying to figure the best way of doing the following.

*I need an array of strings that are globally accessable from within
'arrayhere.h'
*I need that array of strings to not change once initialized 'const'
*I would like to initialize said array with a one liner, but if not
something extremly easy to read.
*I need to know how many elements there are in the array so I can stop
searching the array once I've reached the end, I would like to do this
with out having to make a 'int size' value, so I've used a magic word
the means stop as the last item in the array, this magic word should
always be at the end of the array if I decide to go this route.

Here is what I have
----------------------------

//this_is_just_a_test.cc
#include<string>
#include<iostream>

std::string array[] = {"This", "is", "a", "test", "everyone.", "stop"};

class InArray {

public:

static bool findWord(std::string _n);
};

bool InArray::findWord(std::string _n) {

for(int x=0; array[x] != "stop"; x++)
if (array[x] == _n)
return true;

return false;
}

int main() {

InArray::findWord("Hello");
InArray::findWord("This");
InArray::findWord("a");
InArray::findWord("no");

return 0;
}

-----------------

I would love if I could initialize with something like:

std::vector<std::string> array = {"This", "is", "what", "I", "would",
"like."};

but of course this isn't possible. So if anyone knows how to get
mostly the same functionallity like the above code, I'd love to here
it.

Cheers!

PS: The words in the array[] are magic words that I'd like to look for
and the what I'm checking it aganist is a bunch of other words that
mean something elsewhere in the program. There won't be a case of
someone passing InArray::findWord("stop"), hence the reason I choose
that word. However, this all seems like ugly hacking.

  #2  
Old August 31st, 2005, 09:05 PM
Marc Mutz
Guest
 
Posts: n/a
Default Re: Best way to initialize an array of strings

slack_justyb wrote:
<snip>[color=blue]
> Here is what I have
> ----------------------------
>
> //this_is_just_a_test.cc
> #include<string>
> #include<iostream>
>
> std::string array[] = {"This", "is", "a", "test",
> "everyone.", "stop"};[/color]

Make this an array of const char *, sort it lexically.
[color=blue]
> class InArray {
>
> public:
>
> static bool findWord(std::string _n);
> };
>
> bool InArray::findWord(std::string _n) {
>
> for(int x=0; array[x] != "stop"; x++)
> if (array[x] == _n)
> return true;
>
> return false;
> }[/color]

Then implement this as:

bool InArray::findWord( const std::string & str ) const {
static const unsigned int arraySize =
sizeof array / sizeof *array ;
return std::binary_search(array,array+arraySize,str);
}

Marc

  #3  
Old September 1st, 2005, 02:25 PM
Gabriel
Guest
 
Posts: n/a
Default Re: Best way to initialize an array of strings

slack_justyb wrote:[color=blue]
> Hello,
>
> I'm trying to figure the best way of doing the following.
>
> *I need an array of strings that are globally accessable from within
> 'arrayhere.h'
> *I need that array of strings to not change once initialized 'const'
> *I would like to initialize said array with a one liner, but if not
> something extremly easy to read.
> *I need to know how many elements there are in the array so I can stop
> searching the array once I've reached the end, I would like to do this
> with out having to make a 'int size' value, so I've used a magic word
> the means stop as the last item in the array, this magic word should
> always be at the end of the array if I decide to go this route.
>
> (snip)
>
> std::string array[] = {"This", "is", "a", "test", "everyone.", "stop"};
>
> (snip)[/color]

You don't need an array. Do not use an array unless you have to.
Do something like this:

// header.h
const std::vector<std::string>& magic_words();

// source.cpp
namespace // do not pollute the global namespace
{
std::vector<std::string> init_magic_words()
{
std::vector<std::string> result;
result.push_back("This");
...
result.push_back("everyone.");
return result;
}
} // unnamed namespace

const std::vector<std::string>& magic_words()
{
static const std::vector<std::string>
magic_words_data = init_magic_words();
return magic_words_data;
}

std::vector has size(), checked access etc.
It is much more safe and convenient.

Gabriel
 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles