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