开发者

Does C++ support constant arrays of type string?

I'm a programming student in my first C++ class, and for a r开发者_运维问答ecent project I did, I was unable to create an array of strings like I could do in C#:

string MONTHS[ARRAY_CAPACITY] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
// this yields many compiler errors in C++

Is it possible to do something similar in C++?

Thanks!


If you initialise the array in C++ then it doesn't require a size to be set (although it'll accept one), so:

 std::string months[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };

compiles fine with g++ for me and I'd expect it to compile elsewhere too. I expect your errors are due to the lack of std:: namespace.


Yes, it does:

#include <string>

int main(void)
{
    static const size_t Capacity = 12;
    std::string Months[Capacity] = { "Jan", "Feb", "Mar", "April", "May",
                                        "June", "July", "Aug", "Sep", "Oct",
                                        "Nov", "Dec" };
}

Your errors were probably related to something else. Did you remember to use std::? Without knowing, it could be anything. Was Capacity the wrong size? Etc.

Note your code wasn't actually a constant array. This is:

#include <string>

int main(void)
{
    static const size_t Capacity = 12;
    static const std::string Months[Capacity] = { "Jan", "Feb", "Mar", "April",
 /* ^^^^^^^^^^^^ */                                 "May", "June", "July", "Aug",
                                                    "Sep", "Oct", "Nov", "Dec" };
}

Also, you don't actually need Capacity, as others will show, and you could use const char* if you'd like, though you lose the std::string interface.


The preferred method for an array of constant strings would probably be an array of cstrings,

const char* MONTHS[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" };

However, it can also be done with std::strings,

const string MONTHS[] = { string("Jan"), string("Feb"), ... };

Some compilers may not allow implicit conversion from char* to std::string when you initialize an array with curly braces; explicitly assigning an std::string constructed from a char* will fix that.


Yes. The syntax you used in the question is correct, as long as the compiler understands that string is std::string and as long as the number of initializers in between {} does not exceed ARRAY_CAPACITY.

Of course, if you wanted to have a constant array, as the title suggests, you should have declared it const. Without const your array will have external linkage and cause linker errors if you put it into a header file included into multiple translation units.

const std::string MONTHS[ARRAY_CAPACITY] = { "Jan", /* and so on ... */ };


Yes, Visual C++ supports it - I've just done something similar. Not sure about other versions of C++.

Have you included the library? What is the definition of ARRAY_CAPACITY?

When you say 'unable', do you mean you had compiler/linker errors or something else? Can you provide more details?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜