简体   繁体   English

std::array 初始值设定项如何为字符工作?

[英]How does std::array initializer work for char's?

I'm not sure how the following code works.我不确定以下代码是如何工作的。 I thought you had to do {'h', 'e' ...etc...} but it seems to work fine.我以为你必须做{'h', 'e' ...etc...}但它似乎工作正常。 On the other hand if you do std::array<const char* it only adds one element to the array.另一方面,如果您执行std::array<const char*它只会向数组添加一个元素。 Are there special rules for string literal initialization?字符串文字初始化是否有特殊规则?

std::array<char, strlen("hello world!") + 1> s = {"hello world!"};
for (size_t i = 0; i < s.size(); ++i)
{
    std::cout << s[i];
}

Class std::array is an aggregate.std::array是一个聚合。 In this statement:在此声明中:

std::array<char, strlen("hello world!") + 1> s = {"hello world!"};

list initialization is used.使用列表初始化。 As the first and only element of this instantiation of the class std::array is a character array it may be initialized with string literals.由于类std::array的这个实例化的第一个也是唯一的元素是一个字符数组,它可以用字符串文字初始化。

It would be more correctly to use sizeof operator instead of function strlen :使用sizeof运算符而不是函数strlen会更正确:

std::array<char, sizeof( "hello world!" )> s = {"hello world!"};

Also you could write你也可以写

std::array<char, sizeof( "hello world!" )> s = { { "hello world!" } };

because the character array in turn is an aggregate.因为字符数组又是一个集合。

According to the C++ Standard根据 C++ 标准

8.5.2 Character arrays [dcl.init.string] 8.5.2 字符数组[dcl.init.string]

1 An array of narrow character type (3.9.1), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow string literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces (2.14.5). 1 窄字符类型 (3.9.1)、 char16_t数组、 char32_t数组或wchar_t数组的数组可以分别由窄字符串文字、 char16_t字符串文字、 char32_t字符串文字或宽字符串文字初始化,或由适当的- 括在大括号中的类型字符串文字 (2.14.5)。 Successive characters of the value of the string literal initialize the elements of the array.字符串文字值的连续字符初始化数组的元素。

[ Example: [ 例子:

char msg[] = "Syntax error on line %s\n";

Here's a way of achieving this这是实现这一目标的方法

// GOAL
std::array<char, sizeof("Hello")> myarray = {"Hello"};

ie. IE。 initializing std::array with a string literal (yes, it used a macro)用字符串文字初始化 std::array (是的,它使用了一个宏)

// SOLUTION
#define STD_CHAR_ARRAY_INIT(arrayname, string_literal) /*std::array*/<char, sizeof(string_literal)> arrayname = {string_literal}
std::array STD_CHAR_ARRAY_INIT(myarray, "Hello");

Here's some test code:下面是一些测试代码:

#include <iostream>
#include <array>

using std::cout;
using std::ostream;

template<typename T, size_t N>
std::ostream& std::operator<<(std::ostream& os, array<T, N> arr)
{
  {
    size_t cnt = 0;
    char strchar[2] = "x";
    for (const T& c : arr) {
      strchar[0] = c;
      os << "arr[" << cnt << "] = '" << (c == '\0' ? "\\0" :  strchar /*string(1, c)*/  ) << "'\n"
         << '.' << c << '.' << '\n';
      ++cnt;
    }
  }
  return os;
}


#define STD_CHAR_ARRAY_INIT(arrayname, string_literal) /*std::array*/<char, sizeof(string_literal)> arrayname = {string_literal}

int main()
{
  std::array STD_CHAR_ARRAY_INIT(myarray, "Hello");
  cout << myarray << '\n';
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM