相关疑难解决方法(0)

为什么std :: vector和std :: array的C++ initializer_list行为不同?

码:

std::vector<int> x{1,2,3,4};
std::array<int, 4> y{{1,2,3,4}};
Run Code Online (Sandbox Code Playgroud)

为什么我需要std :: array的双花括号?

c++ stl c++11

67
推荐指数
2
解决办法
5149
查看次数

何时可以在初始化列表中省略外括号?

编译下面的代码时,我在VC2010中遇到错误C2078.

struct A
  {
  int foo;
  double bar;
  };

std::array<A, 2> a1 = 
  // error C2078: too many initializers
  {
    {0, 0.1},
    {2, 3.4}
  };

// OK
std::array<double, 2> a2 = {0.1, 2.3};
Run Code Online (Sandbox Code Playgroud)

我发现正确的语法a1

std::array<A, 2> a1 = 
  {{
    {0, 0.1},
    {2, 3.4}
  }};
Run Code Online (Sandbox Code Playgroud)

问题是:为什么需要额外的括号a1但不是必需的a2

更新

这个问题似乎并不特定于std :: array.一些例子:

struct B
  {
  int foo[2];
  };

// OK
B meow1 = {1,2};
B bark1 = {{1,2}};

struct C
  {
  struct 
    { 
    int a, b; 
    } …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++-2010 c++11

47
推荐指数
1
解决办法
6797
查看次数

C++向量的数组

为什么这样做:

std::pair<int, int> p = {1,2};
std::vector<std::pair<int, int>> vp = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)

但这不是吗?

std::array<int, 2> a = {1,2}; // still ok
std::vector<std::array<int, 2>> va = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)

使用g ++ 4.5.1 -std=c++0x,第二行失败:

错误:无法转换‘{{1, 2}, {3, 4}}’‘std::vector<std::array<int, 2u> >’

谢谢

c++ arrays initializer-list c++11

28
推荐指数
1
解决办法
1万
查看次数

初始化std :: array <>

请考虑以下代码:

#include <array>

struct A
{
    int a;
    int b;
};

static std::array<A, 4> x1 =
{
        { 1, 2 },
        { 3, 4 },
        { 5, 6 },
        { 7, 8 }
};

static std::array<A, 4> x2 =
{
    {
        { 1, 2 },
        { 3, 4 },
        { 5, 6 },
        { 7, 8 }
    }
};

static std::array<A, 4> x3 =
{
       A{ 1, 2 },
       A{ 3, 4 },
       A{ 5, 6 },
       A{ 7, 8 …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-initialization c++11 stdarray

15
推荐指数
1
解决办法
1692
查看次数

std :: array <T>初始化

A std::array<T>本质上是一个包裹在一个C风格的数组struct.structs 的初始化需要大括号,并且数组的初始化也需要大括号.所以我需要两对括号:

std::array<int, 5> a = {{1, 2, 3, 4, 5}};
Run Code Online (Sandbox Code Playgroud)

但是我见过的大多数示例代码只使用了一对大括号:

std::array<int, 5> b = {1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)

为什么这是允许的,与第一个approch相比,它有任何好处或缺点吗?

c++ arrays initialization curly-braces c++11

12
推荐指数
1
解决办法
3575
查看次数

具有私有构造函数和自身静态数组的类

对不起,如果标题令人困惑,我找不到一个简单的方法来写一个简单的句子.无论如何,我面临的问题是:

 // header:
class SomeThing
{
 private:
   SomeThing() {} // <- so users of this class can't come up
                  //    with non-initialized instances, but
                  //    but the implementation can.

   int some_data; // <- a few bytes of memory, the default
                  //    constructor SomeThing() doesn't initialize it
 public:
   SomeThing(blablabla ctor arguments);

   static SomeThing getThatThing(blablabla arguments);

   static void generateLookupTables();
 private:

   // declarations of lookup tables
   static std::array<SomeThing, 64> lookup_table_0;
   static SomeThing lookup_table_1[64];
};
Run Code Online (Sandbox Code Playgroud)

getThatThing函数用于从查找表返回实例.

 // in the implementation file - …
Run Code Online (Sandbox Code Playgroud)

c++ initialization static-initialization c++11 stdarray

8
推荐指数
2
解决办法
1021
查看次数

推导出 std::array 大小?

在以下代码中:

template<size_t N>
int b(int q, const std::array<int, N>& types)
{
    int r = q;
    for (int t : types)
    {
        r = r + t;
    }
    return r;
}

int main()
{
    b<2>(9, { 2,3 });
}
Run Code Online (Sandbox Code Playgroud)

如何避免在调用 b 时为 N 指定 2?为什么不能自动推导出这种类型?没有它我得到错误:

“b”:找不到匹配的重载函数“int b(int,const std::array &)”:无法推导出“N”的模板参数

c++ c++14

8
推荐指数
3
解决办法
5004
查看次数

为什么我不能像这样对std :: array进行脱盐?

为什么我不能像这样对std :: array进行脱盐?

#include <array>

struct Point
{
    float x;
    float y;
};

int main()
{
   std::array<Point, 3> m_points { 
      { 1.0f, 1.0f }, 
      { 2.0f, 2.0f }, 
      { 3.0f, 3.0f }
   };
}
Run Code Online (Sandbox Code Playgroud)

这样做我得到错误:

错误:初始化程序太多了 std::array<Point, 3ul>

但它的工作原理如下:

std::array<Point, 3> m_points { 
   Point{ 1.0f, 1.0f }, 
   Point{ 2.0f, 2.0f }, 
   Point{ 3.0f, 3.0f } 
};
Run Code Online (Sandbox Code Playgroud)

相比之下,std::map可以使用以下两种方式初始化:

   std::map<int, int> m1 {std::pair<int, int>{1,2}, std::pair<int, int>{3,4}}; 
   std::map<int, int> m2 {{1,2}, {3,4}};
Run Code Online (Sandbox Code Playgroud)

c++ aggregate initialization declaration c++14

6
推荐指数
1
解决办法
292
查看次数

使用大括号构造一个“std::array”,其中包含的元素少于它所能容纳的元素?

如果我std::array<T, N>用大括号构造 a 并给它少于N项目,那么这些项目是否为零初始化?(或者它们是否保留默认初始化?)如果我给它零个项目(即= {}),那么我相信它会将所有元素零初始化。

我找不到这个简单问题的明确答案。由于std::array在像 一样使用时使用聚合初始化std::array<int, 2> x = { 1 };,因此导致聚合初始化规则的https://en.cppreference.com/w/cpp/language/aggregate_initialization 。在那里,我看到的关于这种情况的唯一提及是“如果指定了数组的大小并且它大于字符串文字中的字符数,则其余字符将被零初始化。” 但这是在“字符数组”部分中,所以看起来通常不是这样。另一方面,用作constexrpUB 检测器表明它们已归零: https: //godbolt.org/z/zE9xKvbrq

有关的:

c++ language-lawyer stdarray

2
推荐指数
1
解决办法
88
查看次数