设置等于 {} 的向量;

Setting a vector equal to {};

下面的代码是一直有效还是compiler/platform-dependent?显然我可以使用值构造函数初始化 edges,但我很好奇当 edges 初始化为大小 0 时复制赋值 operator= 是否在这里工作,然后设置等于 a支撑的 r 值。

它适用于我的 macbook。

std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};

有效(C++11 起)。 std::vector 超载 operator= 占用 std::initializer_list.

Replaces the contents with those identified by initializer list ilist.

并且 std::initializer_list 可以在指定的上下文中从花括号列表构造。

(强调我的)

A std::initializer_list object is automatically constructed when:

  • a braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an std::initializer_list parameter
  • a braced-init-list is used as the right operand of assignment or as a function call argument, and the corresponding assignment operator/function accepts an std::initializer_list parameter
  • a braced-init-list is bound to auto, including in a ranged for loop