Re: cannot recognize vector as a vector, please help

From: Malte Starostik (malte_at_starostik.de)
Date: 03/29/05


Date: Tue, 29 Mar 2005 21:48:13 +0200

John Guo schrieb:
> Thanks Chris.
>
> The only thing I want to achieve is to have an array of constant
> strings predefined. It seems to me that if I use the vector of string
> approach, I have to initialize it by calling a function. This is
> awkward. I am originally using char[][], and init it in the namespace.
>
> Could someone suggest a more elegant approach to do the same thing?

You could use an accessor function:

typedef std::vector< std::string > PatternsMsgs;

const PatternMsgs& patternMsgs()
{
    static PatternMsgs messages;
    static bool initialised = false;
    if ( !initialised )
    {
        // fill the vector
        initialised = true;
    }
    return messages;
}

that is if the messages are constant. Make it return a non-const
reference otherwise.

Cheers,
Malte



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: Returning array of strings through callback from unmanaged to
    ... Is the array of strings a jagged array or fixed length array? ... It is imperative that I am able to return the array of strings from the ... I have control over how many strings I want the unmanaged code to return ... What I want to accomplish is to pass a callback function to unmanaged ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
    (comp.lang.php)
  • Re: sorting and shuffling array
    ... > I need some help with sorting and shuffling array of strings. ... qsort() library function. ...
    (comp.lang.c)