c++ - 无法使用 Microsoft Visual Studio 2012 列出初始化 vector

标签 c++ vector initializer-list list-initialization

我想用值初始化二维 vector ,它给我这个错误:

IntelliSense: initialization with '{...}' is not allowed for object of type "std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>"

我在使用以下时会出现上述错误是什么?

vector<vector<int>> A =
    { { 0, 0, 0, 0, 0, 0 },
      { 0, 1, 2, 2, 4, 1 },
      { 0, 3, 4, 1, 5, 2 },
      { 0, 2, 3, 3, 2, 4 },
      { 0, 4, 1, 5, 4, 6 },
      { 0, 6, 3, 2, 1, 3 } };

最佳答案

MSVS 2012 has very little C++11 support如果我们查看链接到 MSDN 的文章

enter image description here

我们将看到它不支持您正在使用的初始化所需的初始化列表。

我的建议是,如果可以的话,升级你的编译器。 MSVS 2017 拥有最完整的 C++11 支持(除非在新标准中有任何重大更改)。如果您不能这样做,那么您可以使用 C++11 之前的解决方案来声明临时数组并从临时数组初始化 vector 。

关于c++ - 无法使用 Microsoft Visual Studio 2012 列出初始化 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44765709/

相关文章:

c++ - C++ 中运算符 < 重载的问题

c++ - 在 C++ 中对 vector 的 vector 进行排序

c++ - 将 vector<T> 转换为 initializer_list<T>

c - C中数组初始化错误

c++ - 重新编译 libiconv,在已经成功安装后出现 gettext undefined symbol

c++ - 如何从 std::deque 释放内存?

c++ - 动态大小的不可调整大小的数组

c++ - 对 double 的二维 vector 进行排序

C++ - 使用initializer_list作为参数的模板函数

c# - 托管函数在非托管结构(C++、C#)中作为回调函数传递的问题