C++ 是否支持字符串类型的常量数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2185443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:27:15  来源:igfitidea点击:

Does C++ support constant arrays of type string?

c++arrays

提问by Alex

I'm a programming student in my first C++ class, and for a recent project I did, I was unable to create an array of strings like I could do in C#:

我是我的第一个 C++ 课程的编程学生,对于我最近做的一个项目,我无法像在 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++?

是否可以在 C++ 中做类似的事情?

Thanks!

谢谢!

回答by Mike Anchor

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

如果您在 C++ 中初始化数组,则不需要设置大小(尽管它会接受一个),因此:

 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.

对我来说用 g++ 编译得很好,我希望它也能在其他地方编译。我希望您的错误是由于缺少std::命名空间。

回答by GManNickG

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 Capacitythe wrong size? Etc.

您的错误可能与其他事情有关。你记得用过std::吗?在不知道的情况下,它可以是任何东西。是Capacity不是尺寸不对?等等。

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 coulduse const char*if you'd like, though you lose the std::stringinterface.

此外,您实际上并不需要Capacity,正如其他人会显示的那样,如果您愿意,您可以使用const char*,尽管您丢失了std::string界面。

回答by Collin Dauphinee

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

常量字符串数组的首选方法可能是一个 cstring 数组,

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

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

然而,它也可以用 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.

当您使用花括号初始化数组时,某些编译器可能不允许从 char* 隐式转换为 std::string;显式分配从 char* 构造的 std::string 将解决该问题。

回答by GenericMeatUnit

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

是的,Visual C++ 支持它——我刚刚做了类似的事情。不确定其他版本的 C++。

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

你包括图书馆吗?ARRAY_CAPACITY 的定义是什么?

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

当您说“无法”时,您的意思是您遇到了编译器/链接器错误或其他问题?你能提供更多细节吗?

回答by AnT

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

是的。你在提问中使用的语法是正确的,只要编译器理解stringstd::string只要初始化中的数量{}不超过ARRAY_CAPACITY

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

当然,如果你想要一个常量数组,正如标题所暗示的那样,你应该声明它const。如果const您将数组放入包含在多个翻译单元中的头文件,则没有您的数组将具有外部链接并导致链接器错误。

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