Best way to initialize an array of strings

S

slack_justyb

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

Marc Mutz

slack_justyb wrote:
Here is what I have
----------------------------

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

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

Make this an array of const char *, sort it lexically.
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;
}

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
 
G

Gabriel

slack_justyb said:
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)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top