473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Aug 31 '05 #1
2 57973
slack_justyb wrote:
<snip>
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

Aug 31 '05 #2
slack_justyb wrote:
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
Sep 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: uidzer0 | last post by:
Hey everyone, I'm a little confused on the best way to declare character strings. I've seen many different ways to declare them such as: --- char string = "foobar\0"; --- char *string =...
1
by: DC | last post by:
Hi, I am programming a windows service and all went well until I needed to use a simple array of chars which I initialize like this: char test = new char {'\x002F', '\x005E'}; Immediatly...
1
by: MAF | last post by:
Is there a quick way to initialize an array with one value for example int IDs = new int; I want all items to be initialized to -1.
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
9
by: www.brook | last post by:
hi, I have a class class A { const int m_a; const int m_b; } m_a can be initialized at the constructor A():m_a(2)
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.